1use crate::log::{EntryKind, LogEntry, LogUsage, SessionLog, log_usage, record};
9use crate::stats::{CELL_APIS, Cells, SESSION_STATS_MODULE, Stats, parse_session_stats};
10use indexmap::{IndexMap, IndexSet};
11use lanes::lane_names::{LaneOwner, child_lane, frame_lane, parse_lane};
12use serde_json::{Map, Value, json};
13use session_journal::ns;
14use std::collections::{HashMap, HashSet};
15
16pub const SESSION_REPORT_SCHEMA: &str = "ultron.session-report/1";
17const SUBSCRIPTION_PROVIDERS: [&str; 1] = [config::providers::CLAUDE_CODE];
19const COUNTER_SLACK_MS: f64 = 1000.0;
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22enum Lane {
23 Root,
24 Frames,
25 Subagents,
26 Other,
27}
28
29pub const DURABLE_CHILDREN_MODEL: &str = "sub-agents (host ledger)";
33
34fn lane_kind(lane: Option<&str>) -> Lane {
35 match lane {
36 Some("main") => Lane::Root,
37 Some(lane) => match parse_lane(lane) {
38 Some(LaneOwner::Frame(_)) => Lane::Frames,
39 Some(LaneOwner::Child(_)) => Lane::Subagents,
40 None => Lane::Other,
41 },
42 None => Lane::Other,
43 }
44}
45
46#[derive(Clone, Debug, Default)]
47struct Bucket {
48 responses: f64,
49 input: f64,
50 output: f64,
51 cache_read: f64,
52 cache_write: f64,
53 total_tokens: f64,
54 reported_usd: Option<f64>,
55 subscription_usd: Option<f64>,
56 unpriced: f64,
57 unmeasured: f64,
58}
59
60impl Bucket {
61 fn add(&mut self, usage: &LogUsage, subscription: bool) {
62 self.responses += 1.0;
63 self.input += usage.input;
64 self.output += usage.output;
65 self.cache_read += usage.cache_read;
66 self.cache_write += usage.cache_write;
67 self.total_tokens += usage.total_tokens;
68 if subscription {
69 self.subscription_usd = Some(self.subscription_usd.unwrap_or(0.0) + usage.cost.unwrap_or(0.0));
70 } else if let Some(cost) = usage.cost.filter(|c| *c > 0.0) {
71 self.reported_usd = Some(self.reported_usd.unwrap_or(0.0) + cost);
72 } else if usage.total_tokens > 0.0 {
73 self.unpriced += 1.0;
74 }
75 }
76
77 fn add_totals(&mut self, responses: f64, tokens: f64, cost: f64, subscription: bool) {
79 self.responses += responses;
80 self.total_tokens += tokens;
81 if cost > 0.0 {
82 let slot = if subscription {
83 &mut self.subscription_usd
84 } else {
85 &mut self.reported_usd
86 };
87 *slot = Some(slot.unwrap_or(0.0) + cost);
88 } else if tokens > 0.0 {
89 self.unpriced += responses;
90 }
91 }
92
93 fn fields(&self) -> Map<String, Value> {
94 let opt = |v: Option<f64>| v.map_or(Value::Null, jsrt::json::number);
95 let Value::Object(map) = json!({
96 "responses": jsrt::json::number(self.responses),
97 "input": jsrt::json::number(self.input),
98 "output": jsrt::json::number(self.output),
99 "cacheRead": jsrt::json::number(self.cache_read),
100 "cacheWrite": jsrt::json::number(self.cache_write),
101 "totalTokens": jsrt::json::number(self.total_tokens),
102 "cost": {
103 "reportedUsd": opt(self.reported_usd),
104 "subscriptionUsd": opt(self.subscription_usd),
105 "unpricedResponses": jsrt::json::number(self.unpriced),
106 },
107 "unmeasured": jsrt::json::number(self.unmeasured),
108 }) else {
109 unreachable!()
110 };
111 map
112 }
113}
114
115struct Buckets<'s> {
116 subscription: &'s HashSet<&'s str>,
118 lanes: [Bucket; 4],
119 models: IndexMap<String, Bucket>,
120 by_lane: HashMap<String, Bucket>,
121 total: Bucket,
122}
123
124fn provider_of(model: &str) -> &str {
125 model.split_once('/').map_or(model, |(p, _)| p)
126}
127
128impl Buckets<'_> {
129 fn index(kind: Lane) -> usize {
130 kind as usize
131 }
132
133 fn add(&mut self, kind: Lane, model: &str, usage: &LogUsage, lane: Option<&str>) {
134 let subscription = self.subscription.contains(provider_of(model));
135 self.lanes[Self::index(kind)].add(usage, subscription);
136 self.models
137 .entry(model.to_string())
138 .or_default()
139 .add(usage, subscription);
140 self.total.add(usage, subscription);
141 if let Some(lane) = lane {
142 self.by_lane
143 .entry(lane.to_string())
144 .or_default()
145 .add(usage, subscription);
146 }
147 }
148
149 fn add_totals(&mut self, kind: Lane, model: &str, responses: f64, tokens: f64, cost: f64, subscription: bool) {
150 self.lanes[Self::index(kind)].add_totals(responses, tokens, cost, subscription);
151 self.models
152 .entry(model.to_string())
153 .or_default()
154 .add_totals(responses, tokens, cost, subscription);
155 self.total.add_totals(responses, tokens, cost, subscription);
156 }
157
158 fn unmeasured(&mut self, kind: Lane, model: &str) {
159 self.lanes[Self::index(kind)].unmeasured += 1.0;
160 self.models.entry(model.to_string()).or_default().unmeasured += 1.0;
161 self.total.unmeasured += 1.0;
162 }
163}
164
165fn plural(count: f64, one: &str) -> String {
166 format!(
167 "{} {}",
168 jsrt::number::to_string(count),
169 if count == 1.0 {
170 one.to_string()
171 } else {
172 format!("{one}s")
173 }
174 )
175}
176
177fn depth_verdict(level: f64, frames: f64, subagents: f64, nested: f64, typed: f64, background: f64) -> String {
179 if level == 0.0 {
180 return "root only".into();
181 }
182 let mut parts = vec![plural(frames, "frame")];
183 parts.push(format!(
184 "{}{}",
185 plural(subagents, "sub-agent"),
186 if nested > 0.0 {
187 format!(" ({} nested)", jsrt::number::to_string(nested))
188 } else {
189 String::new()
190 }
191 ));
192 if typed > 0.0 {
193 parts.push(plural(typed, "typed-agent task"));
194 }
195 if background > 0.0 {
196 parts.push(plural(background, "background job"));
197 }
198 format!("depth {}: {}", jsrt::number::to_string(level), parts.join(", "))
199}
200
201pub fn session_is_empty(log: &SessionLog) -> bool {
203 log.entries.is_empty()
204 && !log.values.keys().any(|address| {
205 let (namespace, key) = address.split_once('/').unwrap_or((address, ""));
206 namespace.starts_with("ultron.")
207 && !(namespace == ns::MODULE && key == "skills")
208 && ![ns::SESSION_NAME, ns::LANE_CONFIG, ns::BRANCH_TIP].contains(&namespace)
209 })
210}
211
212#[derive(Clone, Debug)]
213struct Task {
214 id: String,
215 definition: String,
216 state: String,
217 parent_id: Option<String>,
218 result: Option<Map<String, Value>>,
219}
220
221fn definition_name(definition: Option<&Value>) -> String {
222 let text = match definition {
223 None | Some(Value::Null) => "unknown".to_string(),
224 Some(v) => jsrt::value::string(Some(v)),
225 };
226 if let Some(at) = text.rfind('@') {
228 let digits = &text[at + 1..];
229 if !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()) {
230 return text[..at].to_string();
231 }
232 }
233 text
234}
235
236fn tasks_of(log: &SessionLog) -> Vec<Task> {
237 let list = record(log.value(ns::TASKS, "root"))
238 .and_then(|t| t.get("tasks"))
239 .and_then(Value::as_array);
240 list.into_iter()
241 .flatten()
242 .filter_map(|item| {
243 let task = item.as_object()?;
244 Some(Task {
245 id: task.get("id")?.as_str()?.to_string(),
246 definition: definition_name(task.get("definition")),
247 state: task
248 .get("state")
249 .and_then(Value::as_str)
250 .unwrap_or("unknown")
251 .to_string(),
252 parent_id: task.get("parentId").and_then(Value::as_str).map(str::to_string),
253 result: record(task.get("result")).cloned(),
254 })
255 })
256 .collect()
257}
258
259fn model_name(config: &Value) -> Option<String> {
260 let model = record(Some(config))?.get("model")?.as_object()?;
261 Some(format!(
262 "{}/{}",
263 model.get("provider")?.as_str()?,
264 model.get("modelId")?.as_str()?
265 ))
266}
267
268#[derive(Clone, Default)]
269struct ModelCount {
270 count: f64,
271 tokens: f64,
272 unmeasured: f64,
273}
274
275fn by_model_list(models: IndexMap<String, ModelCount>) -> Value {
276 let mut list: Vec<(String, ModelCount)> = models.into_iter().collect();
277 list.sort_by(|a, b| {
278 b.1.count
279 .partial_cmp(&a.1.count)
280 .unwrap_or(std::cmp::Ordering::Equal)
281 .then_with(|| jsrt::collate::locale_compare(&a.0, &b.0))
282 });
283 Value::Array(
284 list.into_iter()
285 .map(|(model, c)| {
286 json!({"model": model, "count": jsrt::json::number(c.count), "tokens": jsrt::json::number(c.tokens), "unmeasured": jsrt::json::number(c.unmeasured)})
287 })
288 .collect(),
289 )
290}
291
292fn apis_value(apis: &IndexMap<String, f64>) -> Value {
293 let mut ordered = Map::new();
294 for api in CELL_APIS {
295 if let Some(n) = apis.get(api) {
296 ordered.insert(api.to_string(), jsrt::json::number(*n));
297 }
298 }
299 let mut unknown: Vec<&String> = apis.keys().filter(|k| !CELL_APIS.contains(&k.as_str())).collect();
300 jsrt::utf16::sort(&mut unknown);
301 for api in unknown {
302 ordered.insert(api.clone(), jsrt::json::number(apis[api]));
303 }
304 Value::Object(ordered)
305}
306
307fn cells_value(cells: &Cells) -> Value {
308 json!({"count": jsrt::json::number(cells.count), "failed": jsrt::json::number(cells.failed), "apis": apis_value(&cells.apis)})
309}
310
311pub fn build_session_report(log: &SessionLog, subscription_providers: &[String]) -> Value {
314 let subscription: HashSet<&str> = SUBSCRIPTION_PROVIDERS
315 .into_iter()
316 .chain(subscription_providers.iter().map(String::as_str))
317 .collect();
318 let mut unrecorded = Map::new();
319 let tasks = tasks_of(log);
320 let mut task_by_id: HashMap<&str, &Task> = HashMap::new();
321 for task in &tasks {
322 task_by_id.insert(&task.id, task);
323 }
324
325 let mut entry_by_id: HashMap<&str, &LogEntry> = HashMap::new();
327 for entry in &log.entries {
328 entry_by_id.insert(&entry.id, entry);
329 }
330 let mut root_ids: HashMap<String, String> = HashMap::new();
331 let tree_root = |id: &str, root_ids: &mut HashMap<String, String>| -> String {
332 let mut path: IndexSet<String> = IndexSet::new();
333 let mut current = Some(id.to_string());
334 let mut root = id.to_string();
335 while let Some(c) = current {
336 if let Some(known) = root_ids.get(&c) {
337 root = known.clone();
338 break;
339 }
340 path.insert(c.clone());
341 root = c.clone();
342 let parent = entry_by_id.get(c.as_str()).and_then(|e| e.parent_id.clone());
343 current = parent.filter(|p| !path.contains(p));
344 }
345 for visited in path {
346 root_ids.insert(visited, root.clone());
347 }
348 root
349 };
350 let mut tree_lane: HashMap<String, String> = HashMap::new();
351 let mut claim = |entry_id: Option<&str>, lane: &str, root_ids: &mut HashMap<String, String>| {
352 let Some(entry_id) = entry_id.filter(|id| entry_by_id.contains_key(id)) else {
353 return;
354 };
355 let root = tree_root(entry_id, root_ids);
356 if !tree_lane.contains_key(&root) || lane == "main" {
357 tree_lane.insert(root, lane.to_string());
358 }
359 };
360 for (lane, tip) in log.values_of(ns::BRANCH_TIP) {
361 claim(tip.as_str(), lane, &mut root_ids);
362 }
363 for result in &log.results {
364 let Some(op) = log.operations.get(&result.operation_id) else {
365 continue;
366 };
367 claim(result.tip_id.as_deref(), &op.lane, &mut root_ids);
368 claim(result.from_tip_id.as_deref(), &op.lane, &mut root_ids);
369 }
370 let lanes: Vec<Option<String>> = log
371 .entries
372 .iter()
373 .map(|entry| tree_lane.get(&tree_root(&entry.id, &mut root_ids)).cloned())
374 .collect();
375 let mut lane_models: HashMap<&str, String> = HashMap::new();
376 for (lane, config) in log.values_of(ns::LANE_CONFIG) {
377 if let Some(model) = model_name(config) {
378 lane_models.insert(lane, model);
379 }
380 }
381
382 let main_entries: Vec<&LogEntry> = log
384 .entries
385 .iter()
386 .zip(&lanes)
387 .filter(|(_, lane)| lane.as_deref() == Some("main"))
388 .map(|(e, _)| e)
389 .collect();
390 let main_runs: Vec<_> = log
391 .results
392 .iter()
393 .filter(|r| r.kind == "run" && log.operations.get(&r.operation_id).map(|o| o.lane.as_str()) == Some("main"))
394 .collect();
395 let name = log.value(ns::SESSION_NAME, "").and_then(Value::as_str);
396 let empty_roots = Map::new();
397 let usage_roots = record(record(log.value(ns::USAGE, "root")).and_then(|u| u.get("roots"))).unwrap_or(&empty_roots);
398 let external_marks = name == Some("claude code")
399 || name.is_some_and(|n| n.starts_with("claude subagent ("))
400 || usage_roots.keys().any(|id| id.starts_with("turn:cc-"));
401 let mut first_activity = f64::INFINITY;
402 let mut happened = |at: Option<f64>| {
403 if let Some(at) = at
404 && at > 0.0
405 && at < first_activity
406 {
407 first_activity = at;
408 }
409 };
410 for entry in &log.entries {
411 happened(Some(entry.timestamp));
412 }
413 for op in log.operations.values() {
414 happened(Some(op.started_at));
415 }
416 for root in usage_roots.values() {
417 happened(root.get("startedAt").and_then(Value::as_f64));
418 }
419 let nothing_ran = first_activity == f64::INFINITY && tasks.is_empty() && !external_marks;
420 let stats: Option<Stats> = parse_session_stats(log.value(ns::MODULE, SESSION_STATS_MODULE))
421 .or_else(|| nothing_ran.then(|| Stats::empty(log.header.created_at)));
422 let mode = if stats.as_ref().is_some_and(|s| s.external)
423 || (stats.is_none() && main_entries.is_empty() && external_marks)
424 {
425 "ultron claude"
426 } else if !log.values_of("ultron.claude-code.lanes").is_empty()
427 || main_entries
428 .iter()
429 .any(|e| e.kind == EntryKind::Assistant && e.provider.as_deref() == Some(config::providers::CLAUDE_CODE))
430 {
431 "ultron --claude"
432 } else {
433 "ultron"
434 };
435 let external = mode == "ultron claude";
436 let stats_cover = stats
437 .as_ref()
438 .is_some_and(|s| s.since <= first_activity + COUNTER_SLACK_MS);
439
440 let turns = if external {
442 match &stats {
443 None => {
444 unrecorded.insert(
445 "turns".into(),
446 "Claude Code owns the root conversation; this session predates Ultron's turn count".into(),
447 );
448 Value::Null
449 }
450 Some(s) => json!({
451 "count": jsrt::json::number(s.external_turns.0),
452 "completed": jsrt::json::number(s.external_turns.0),
453 "aborted": 0,
454 "failed": 0,
455 "running": 0,
456 "wallMs": jsrt::json::number(s.external_turns.1),
457 }),
458 }
459 } else {
460 let ended: HashSet<&str> = log.results.iter().map(|r| r.operation_id.as_str()).collect();
461 let running = log
462 .operations
463 .values()
464 .filter(|o| o.lane == "main" && o.kind == "run" && !ended.contains(o.id.as_str()))
465 .count() as f64;
466 let with = |f: &dyn Fn(&str) -> bool| main_runs.iter().filter(|r| f(&r.status)).count() as f64;
467 json!({
468 "count": jsrt::json::number(main_runs.len() as f64 + running),
469 "completed": jsrt::json::number(with(&|s| s == "completed")),
470 "aborted": jsrt::json::number(with(&|s| s == "aborted")),
471 "failed": jsrt::json::number(with(&|s| s == "failed" || s == "declined")),
472 "running": jsrt::json::number(running),
473 "wallMs": jsrt::json::number(main_runs.iter().fold(0.0, |t, r| t + (r.ended_at - r.started_at).max(0.0))),
474 })
475 };
476
477 let mut buckets = Buckets {
479 subscription: &subscription,
480 lanes: Default::default(),
481 models: IndexMap::new(),
482 by_lane: HashMap::new(),
483 total: Bucket::default(),
484 };
485 let mut root_models: IndexMap<String, f64> = IndexMap::new();
486 let mut attributed: HashSet<&str> = HashSet::new();
487 for (entry, lane) in log.entries.iter().zip(&lanes) {
488 let Some(usage) = &entry.usage else { continue };
489 let lane = lane.as_deref();
490 let kind = lane_kind(lane);
491 if entry.kind == EntryKind::Assistant {
492 let model = format!(
493 "{}/{}",
494 entry.provider.as_deref().unwrap_or("unknown"),
495 entry.model.as_deref().unwrap_or("unknown")
496 );
497 buckets.add(kind, &model, usage, lane);
498 if kind == Lane::Root {
499 *root_models.entry(model).or_default() += 1.0;
500 }
501 } else {
502 let model = lane
503 .and_then(|l| lane_models.get(l))
504 .map_or("unknown (summaries)", String::as_str);
505 buckets.add(kind, model, usage, lane);
506 }
507 attributed.insert(&entry.id);
508 }
509 let lane_of_id: HashMap<&str, Option<&str>> = log
510 .entries
511 .iter()
512 .zip(&lanes)
513 .map(|(e, l)| (e.id.as_str(), l.as_deref()))
514 .collect();
515 for row in &log.usage_rows {
516 if row.entry_id.as_deref().is_some_and(|id| attributed.contains(id)) {
517 continue;
518 }
519 match row.entry_id.as_deref().filter(|id| entry_by_id.contains_key(id)) {
520 None => buckets.add(
521 Lane::Other,
522 if row.adjustment {
523 "imported history"
524 } else {
525 "unattributed"
526 },
527 &row.usage,
528 None,
529 ),
530 Some(id) => {
531 let lane = lane_of_id.get(id).copied().flatten();
532 let model = lane.and_then(|l| lane_models.get(l)).map_or("unknown", String::as_str);
533 buckets.add(lane_kind(lane), model, &row.usage, lane);
534 }
535 }
536 }
537 let mut child_models: HashMap<&str, String> = HashMap::new();
538 let ledger_calls: Vec<&Value> = usage_roots
539 .values()
540 .filter_map(|root| root.get("calls").and_then(Value::as_array))
541 .flatten()
542 .collect();
543 let mut process_children: HashSet<&str> = HashSet::new();
544 for task in &tasks {
545 if !external || task.definition != "rlm-child" || lane_models.contains_key(child_lane(&task.id).as_str()) {
546 continue;
547 }
548 process_children.insert(&task.id);
549 let model = stats
550 .as_ref()
551 .and_then(|s| s.child_models.get(&task.id).cloned())
552 .unwrap_or_else(|| "claude-code/(model not recorded)".into());
553 child_models.insert(&task.id, model.clone());
554 let call = ledger_calls.iter().find(|item| {
555 item.get("kind").and_then(Value::as_str) == Some("model")
556 && item.get("taskId").and_then(Value::as_str) == Some(&task.id)
557 });
558 let measured = record(call.and_then(|c| c.get("usage")));
559 let Some(measured) = measured.filter(|m| m.get("totalTokens").is_some_and(Value::is_number)) else {
560 if task.state != "admitted" && task.state != "running" {
561 buckets.unmeasured(Lane::Subagents, &model);
562 }
563 continue;
564 };
565 let mut usage = Map::new();
566 for (to, from) in [
567 ("input", "inputTokens"),
568 ("output", "outputTokens"),
569 ("totalTokens", "totalTokens"),
570 ] {
571 if let Some(v) = measured.get(from) {
572 usage.insert(to.into(), v.clone());
573 }
574 }
575 if let Some(cost) = measured.get("cost").filter(|c| c.is_number()) {
576 usage.insert("cost".into(), json!({"total": cost}));
577 }
578 if let Some(usage) = log_usage(Some(&Value::Object(usage))) {
579 buckets.add(Lane::Subagents, &model, &usage, Some(&child_lane(&task.id)));
580 }
581 }
582
583 let mut durable_other = Bucket::default();
588 {
589 for response in &log.durable_responses {
590 let kind = match lane_kind(response.lane.as_deref()) {
591 Lane::Frames => Lane::Frames,
592 _ if response.owned => Lane::Subagents,
593 _ => Lane::Other,
594 };
595 buckets.add(kind, &response.model, &response.usage, response.lane.as_deref());
596 if kind == Lane::Other {
597 let notional = subscription.contains(provider_of(&response.model));
598 durable_other.add(&response.usage, notional);
599 }
600 }
601 }
602
603 if !external {
607 let field = |root: &Value, name: &str| {
608 root.get("turns")
609 .and_then(|t| t.get(name))
610 .and_then(Value::as_f64)
611 .unwrap_or(0.0)
612 };
613 let (ledger_responses, ledger_tokens, ledger_cost) =
614 usage_roots.values().fold((0.0, 0.0, 0.0), |(r, t, c), root| {
615 (
616 r + field(root, "count"),
617 t + field(root, "tokens"),
618 c + field(root, "cost"),
619 )
620 });
621 let mut seen: Vec<&Bucket> = [Lane::Root, Lane::Frames, Lane::Subagents]
622 .map(|kind| &buckets.lanes[Buckets::index(kind)])
623 .to_vec();
624 seen.push(&durable_other);
625 let seen_tokens: f64 = seen.iter().map(|b| b.total_tokens).sum();
626 let seen_responses: f64 = seen.iter().map(|b| b.responses).sum();
627 let seen_cost: f64 = seen
628 .iter()
629 .map(|b| b.reported_usd.unwrap_or(0.0) + b.subscription_usd.unwrap_or(0.0))
630 .sum();
631 if ledger_tokens > seen_tokens && ledger_responses > seen_responses {
632 let notional = root_models
633 .keys()
634 .next()
635 .is_some_and(|model| subscription.contains(provider_of(model)));
636 buckets.add_totals(
637 Lane::Subagents,
638 DURABLE_CHILDREN_MODEL,
639 ledger_responses - seen_responses,
640 ledger_tokens - seen_tokens,
641 (ledger_cost - seen_cost).max(0.0),
642 notional,
643 );
644 }
645 }
646
647 let mut transcript: [Cells; 3] = Default::default();
649 let mut other_tools: IndexMap<String, (f64, f64)> = IndexMap::new();
650 let mut redactions = 0.0;
651 let mut nudges = [0.0; 3];
652 for (entry, lane) in log.entries.iter().zip(&lanes) {
653 let kind = match lane_kind(lane.as_deref()) {
654 Lane::Root => 0,
655 Lane::Subagents => 1,
656 Lane::Frames | Lane::Other => 2,
657 };
658 match entry.kind {
659 EntryKind::Assistant => {
660 for call in &entry.tool_calls {
661 if call.name == "rlm" {
662 transcript[kind].count += 1.0;
663 for api in call.apis.iter().flatten() {
664 *transcript[kind].apis.entry(api.to_string()).or_default() += 1.0;
665 }
666 } else {
667 other_tools.entry(call.name.clone()).or_default().0 += 1.0;
668 }
669 }
670 }
671 EntryKind::ToolResult => {
672 redactions += entry.redactions;
673 if !entry.is_error {
674 continue;
675 }
676 match entry.tool_name.as_deref() {
677 Some("rlm") => transcript[kind].failed += 1.0,
678 Some(name) => other_tools.entry(name.to_string()).or_default().1 += 1.0,
679 None => {}
680 }
681 }
682 EntryKind::User => {
683 if let Some(nudge) = entry.nudge {
684 nudges[["toolRounds", "wait", "skill"]
685 .iter()
686 .position(|n| *n == nudge)
687 .unwrap_or(0)] += 1.0;
688 }
689 }
690 _ => {}
691 }
692 }
693 let other_tools_value = Value::Object(
694 other_tools
695 .iter()
696 .map(|(name, (calls, failed))| {
697 (
698 name.clone(),
699 json!({"calls": jsrt::json::number(*calls), "failed": jsrt::json::number(*failed)}),
700 )
701 })
702 .collect(),
703 );
704 let cells_from = |source: &str, by: &[Cells; 3]| -> Value {
705 let mut total = Cells::default();
706 for part in by {
707 total.count += part.count;
708 total.failed += part.failed;
709 for (api, n) in &part.apis {
710 *total.apis.entry(api.clone()).or_default() += n;
711 }
712 }
713 json!({
714 "source": source,
715 "total": cells_value(&total),
716 "root": cells_value(&by[0]),
717 "subagents": cells_value(&by[1]),
718 "other": cells_value(&by[2]),
719 "otherTools": other_tools_value,
720 })
721 };
722 let cells = if !external {
723 cells_from("transcript", &transcript)
724 } else if let Some(s) = &stats {
725 cells_from("counters", &s.cells)
726 } else {
727 unrecorded.insert(
728 "cells".into(),
729 "Claude Code owns the transcript; this session predates Ultron's cell count".into(),
730 );
731 Value::Null
732 };
733
734 let children: Vec<&Task> = tasks.iter().filter(|t| t.definition == "rlm-child").collect();
736 let child_depth = |task: &Task| -> f64 {
737 let mut depth = 0.0;
738 let mut seen: HashSet<&str> = HashSet::new();
739 let mut current = Some(task);
740 while let Some(c) = current {
741 if seen.contains(c.id.as_str()) {
742 break;
743 }
744 seen.insert(&c.id);
745 if c.definition == "rlm-child" {
746 depth += 1.0;
747 }
748 current = c.parent_id.as_deref().and_then(|p| task_by_id.get(p).copied());
749 }
750 depth
751 };
752 let level_of_task =
753 |id: Option<&str>| -> f64 { id.and_then(|i| task_by_id.get(i)).map_or(0.0, |t| child_depth(t)) };
754 let state =
755 |items: &[&Task], states: &[&str]| items.iter().filter(|t| states.contains(&t.state.as_str())).count() as f64;
756 let mut verdicts = [0.0; 6];
758 let mut claims = [0.0; 3];
760 let mut worktrees = Vec::new();
761 let mut child_by_model: IndexMap<String, ModelCount> = IndexMap::new();
762 let mut durable_models: HashMap<&str, &str> = HashMap::new();
764 for response in &log.durable_responses {
765 if let Some(lane) = &response.lane {
766 durable_models.entry(lane.as_str()).or_insert(response.model.as_str());
767 }
768 }
769 for child in &children {
770 let lane = child_lane(&child.id);
771 let model = lane_models
772 .get(lane.as_str())
773 .cloned()
774 .or_else(|| durable_models.get(lane.as_str()).map(|m| m.to_string()))
775 .or_else(|| child_models.get(child.id.as_str()).cloned())
776 .or_else(|| stats.as_ref().and_then(|s| s.child_models.get(&child.id).cloned()))
777 .unwrap_or_else(|| "not recorded".into());
778 let tokens = buckets.by_lane.get(&lane).map(|b| b.total_tokens);
779 let by_model = child_by_model.entry(model).or_default();
780 by_model.count += 1.0;
781 by_model.tokens += tokens.unwrap_or(0.0);
782 if tokens.is_none() && process_children.contains(child.id.as_str()) && child.result.is_some() {
783 by_model.unmeasured += 1.0;
784 }
785 let Some(result) = &child.result else { continue };
786 let verdict = record(result.get("verdict"));
787 let outcome = record(result.get("check"))
788 .and_then(|c| c.get("outcome"))
789 .and_then(Value::as_str);
790 match (verdict, outcome) {
791 (None, Some("invalid")) => verdicts[3] += 1.0,
792 (None, _) => verdicts[5] += 1.0,
793 (Some(_), Some("verified")) => verdicts[0] += 1.0,
794 (Some(_), Some("contradicted")) => verdicts[1] += 1.0,
795 (Some(_), _) => verdicts[4] += 1.0,
796 }
797 match verdict.and_then(|v| v.get("status")).and_then(Value::as_str) {
798 Some("passed") => claims[0] += 1.0,
799 Some("failed") => claims[1] += 1.0,
800 Some("blocked") => claims[2] += 1.0,
801 _ => {}
802 }
803 let worktree = record(result.get("worktree"));
804 if let Some(w) = worktree
805 && let Some(branch) = w.get("branch").and_then(Value::as_str)
806 {
807 let merge = stats
808 .as_ref()
809 .and_then(|s| s.merges.get(&child.id).cloned())
810 .map(Value::String)
811 .unwrap_or_else(|| {
812 if w.get("commit") == Some(&Value::Null) {
813 Value::String("empty".into())
814 } else {
815 Value::Null
816 }
817 });
818 worktrees.push(json!({
819 "task": child.id,
820 "branch": branch,
821 "changedFiles": jsrt::json::number(w.get("changed_files").and_then(Value::as_array).map_or(0, Vec::len) as f64),
822 "merge": merge,
823 }));
824 }
825 }
826 verdicts[2] = verdicts[3] + verdicts[4] + verdicts[5];
827 let depths: Vec<f64> = children.iter().map(|c| child_depth(c)).collect();
828 let max_child_depth = depths.iter().fold(0.0_f64, |m, d| m.max(*d));
829
830 let mut trace_by_id: IndexMap<String, &Map<String, Value>> = IndexMap::new();
832 for (key, value) in log.values_of(ns::RLM_FRAMES) {
833 if key == "index" {
834 continue;
835 }
836 if let Some(trace) = value.as_object()
837 && let Some(id) = trace.get("id").and_then(Value::as_str)
838 {
839 trace_by_id.insert(id.to_string(), trace);
840 }
841 }
842 let frame_tasks: Vec<&Task> = tasks.iter().filter(|t| t.definition == "rlm-frame").collect();
843 let mut traced: HashSet<&str> = HashSet::new();
844 let mut frames = [0.0; 7];
846 let mut incomplete_reasons: IndexMap<String, f64> = IndexMap::new();
847 let mut budget_tokens = 0.0;
848 let mut frame_by_model: IndexMap<String, ModelCount> = IndexMap::new();
849 let mut frame_levels: HashMap<String, f64> = HashMap::new();
850 fn frame_level(
851 trace: &Map<String, Value>,
852 seen: &mut HashSet<String>,
853 levels: &mut HashMap<String, f64>,
854 traces: &IndexMap<String, &Map<String, Value>>,
855 level_of_task: &dyn Fn(Option<&str>) -> f64,
856 ) -> f64 {
857 let id = trace
858 .get("id")
859 .map(|v| jsrt::value::string(Some(v)))
860 .unwrap_or_default();
861 if let Some(known) = levels.get(&id) {
862 return *known;
863 }
864 seen.insert(id.clone());
865 let parent = trace
866 .get("parentFrame")
867 .and_then(Value::as_str)
868 .filter(|p| !seen.contains(*p))
869 .and_then(|p| traces.get(p).copied());
870 let level = match parent {
871 Some(parent) => frame_level(parent, seen, levels, traces, level_of_task) + 1.0,
872 None => level_of_task(trace.get("callerTaskId").and_then(Value::as_str)) + 1.0,
873 };
874 levels.insert(id, level);
875 level
876 }
877 let mut max_frame_level: f64 = 0.0;
878 let count_frame = |model: String, lane: Option<&str>, by_model: &mut IndexMap<String, ModelCount>| {
879 let tokens = lane.and_then(|l| buckets.by_lane.get(l)).map(|b| b.total_tokens);
880 let entry = by_model.entry(model).or_default();
881 entry.count += 1.0;
882 entry.tokens += tokens.unwrap_or(0.0);
883 };
884 for trace in trace_by_id.values() {
885 frames[0] += 1.0;
886 if let Some(task_id) = trace.get("taskId").and_then(Value::as_str) {
887 traced.insert(task_id);
888 }
889 match trace.get("status").and_then(Value::as_str) {
890 Some("complete") => frames[1] += 1.0,
891 Some("incomplete") => {
892 frames[2] += 1.0;
893 let reason = trace.get("reason").and_then(Value::as_str).unwrap_or("unknown");
894 *incomplete_reasons.entry(reason.to_string()).or_default() += 1.0;
895 }
896 Some("error") => frames[3] += 1.0,
897 _ => frames[5] += 1.0,
898 }
899 if let Some(spent) = record(trace.get("spent"))
900 .and_then(|s| s.get("tokens"))
901 .and_then(Value::as_f64)
902 && spent.is_finite()
903 {
904 budget_tokens += spent;
905 }
906 let level = frame_level(
907 trace,
908 &mut HashSet::new(),
909 &mut frame_levels,
910 &trace_by_id,
911 &level_of_task,
912 );
913 if level > 1.0 {
914 frames[6] += 1.0;
915 }
916 max_frame_level = max_frame_level.max(level);
917 let lane = match (
918 trace.get("lane").and_then(Value::as_str),
919 trace.get("taskId").and_then(Value::as_str),
920 ) {
921 (Some(lane), _) => Some(lane.to_string()),
922 (None, Some(task)) => Some(frame_lane(task)),
923 _ => None,
924 };
925 let model = trace
926 .get("model")
927 .and_then(Value::as_str)
928 .map(str::to_string)
929 .or_else(|| lane.as_deref().and_then(|l| lane_models.get(l).cloned()))
930 .unwrap_or_else(|| "not recorded".into());
931 count_frame(model, lane.as_deref(), &mut frame_by_model);
932 }
933 for task in &frame_tasks {
934 if traced.contains(task.id.as_str()) {
935 continue;
936 }
937 frames[0] += 1.0;
938 match task.state.as_str() {
939 "completed" => frames[1] += 1.0,
940 "cancelled" | "interrupted" => frames[4] += 1.0,
941 "failed" => frames[3] += 1.0,
942 _ => frames[5] += 1.0,
943 }
944 let level = level_of_task(task.parent_id.as_deref()) + 1.0;
945 if level > 1.0 {
946 frames[6] += 1.0;
947 }
948 max_frame_level = max_frame_level.max(level);
949 let lane = frame_lane(&task.id);
950 let model = lane_models
951 .get(lane.as_str())
952 .cloned()
953 .unwrap_or_else(|| "not recorded".into());
954 count_frame(model, Some(&lane), &mut frame_by_model);
955 }
956
957 let typed: Vec<&Task> = tasks
958 .iter()
959 .filter(|t| !["rlm-child", "rlm-frame", "background-job"].contains(&t.definition.as_str()))
960 .collect();
961 let mut by_definition: IndexMap<String, f64> = IndexMap::new();
962 for task in &typed {
963 *by_definition.entry(task.definition.clone()).or_default() += 1.0;
964 }
965 let background: Vec<&Task> = tasks.iter().filter(|t| t.definition == "background-job").collect();
966 let other_level = typed
967 .iter()
968 .chain(&background)
969 .fold(0.0_f64, |m, t| m.max(level_of_task(t.parent_id.as_deref()) + 1.0));
970 let level = max_child_depth.max(max_frame_level).max(other_level);
971 let nested_children = depths.iter().filter(|d| **d > 1.0).count() as f64;
972 if !stats_cover {
973 let reason = if stats.is_none() {
974 "this session predates Ultron's runtime counters"
975 } else {
976 "the runtime counters began after this session did"
977 };
978 unrecorded.insert("depth.workflows".into(), reason.into());
979 unrecorded.insert("depth.frames.calls".into(), reason.into());
980 }
981
982 let mut hints: IndexMap<String, f64> = IndexMap::new();
984 if let Some(lanes) = record(record(log.value(ns::MODULE, "hints")).and_then(|h| h.get("lanes"))) {
985 for lane in lanes.values() {
986 if let Some(fired) = record(lane.get("fired")) {
987 for (tag, n) in fired {
988 if let Some(n) = n.as_f64().filter(|n| *n > 0.0) {
989 *hints.entry(tag.clone()).or_default() += n;
990 }
991 }
992 }
993 }
994 }
995 if stats.is_none() {
996 unrecorded.insert(
997 "guardrails.guards".into(),
998 "kept only in the worker's memory when this session ran".into(),
999 );
1000 unrecorded.insert(
1001 "guardrails.usageLimitBlocks".into(),
1002 "this session predates the count".into(),
1003 );
1004 }
1005 let nudges_value = |n: &[f64; 3]| json!({"toolRounds": jsrt::json::number(n[0]), "wait": jsrt::json::number(n[1]), "skill": jsrt::json::number(n[2])});
1006 let (secrets_masked, reported_nudges) = if !external {
1007 (jsrt::json::number(redactions), nudges_value(&nudges))
1008 } else if let Some(s) = &stats {
1009 (jsrt::json::number(s.secrets_masked), nudges_value(&s.nudges))
1010 } else {
1011 unrecorded.insert(
1012 "guardrails.secretsMasked".into(),
1013 "cell output went to Claude Code; this session predates the count".into(),
1014 );
1015 unrecorded.insert(
1016 "guardrails.nudges".into(),
1017 "steers went to Claude Code with a cell result; this session predates the count".into(),
1018 );
1019 (Value::Null, Value::Null)
1020 };
1021
1022 let memory = record(log.value(ns::MEMORY_STATE, "root"))
1024 .and_then(|m| m.get("operations"))
1025 .and_then(Value::as_array)
1026 .filter(|ops| !ops.is_empty());
1027 let recalls: Vec<_> = log.entries.iter().filter_map(|entry| entry.recall).collect();
1030 let recall = (!recalls.is_empty()).then(|| {
1031 json!({
1032 "messages": recalls.len(),
1033 "memories": recalls.iter().map(|r| r.memories).sum::<usize>(),
1034 "briefs": recalls.iter().filter(|r| r.brief).count(),
1035 "stale": recalls.iter().map(|r| r.stale).sum::<usize>(),
1036 })
1037 });
1038 let operations = match memory {
1039 Some(ops) => {
1040 let mut counted: IndexMap<String, f64> = IndexMap::new();
1041 for op in ops {
1042 let field = |k: &str| match op.as_object().and_then(|o| o.get(k)) {
1043 None | Some(Value::Null) => "unknown".to_string(),
1044 Some(v) => jsrt::value::string(Some(v)),
1045 };
1046 *counted
1047 .entry(format!("{}.{}", field("kind"), field("state")))
1048 .or_default() += 1.0;
1049 }
1050 counts_value(&counted)
1051 }
1052 None => {
1053 unrecorded.insert(
1054 "memory.operations".into(),
1055 if recall.is_some() {
1056 "the model made no memory call; automatic recall is below".into()
1057 } else {
1058 "no memory operation recorded: Hindsight not configured, or memory unused".into()
1059 },
1060 );
1061 Value::Null
1062 }
1063 };
1064 if external {
1065 unrecorded.insert(
1066 "root.models".into(),
1067 "Claude Code owns the root conversation: its model, tokens and cost are Claude Code's".into(),
1068 );
1069 }
1070
1071 let mut root_model_list: Vec<(String, f64)> = root_models.into_iter().collect();
1072 root_model_list.sort_by(|a, b| {
1073 b.1.partial_cmp(&a.1)
1074 .unwrap_or(std::cmp::Ordering::Equal)
1075 .then_with(|| jsrt::collate::locale_compare(&a.0, &b.0))
1076 });
1077 let mut models: Vec<(String, Bucket)> = buckets.models.clone().into_iter().collect();
1078 models.sort_by(|a, b| {
1079 b.1.total_tokens
1080 .partial_cmp(&a.1.total_tokens)
1081 .unwrap_or(std::cmp::Ordering::Equal)
1082 .then_with(|| jsrt::collate::locale_compare(&a.0, &b.0))
1083 });
1084 let mut frames_value = json!({
1085 "count": jsrt::json::number(frames[0]),
1086 "complete": jsrt::json::number(frames[1]),
1087 "incomplete": jsrt::json::number(frames[2]),
1088 "failed": jsrt::json::number(frames[3]),
1089 "cancelled": jsrt::json::number(frames[4]),
1090 "running": jsrt::json::number(frames[5]),
1091 "nested": jsrt::json::number(frames[6]),
1092 "incompleteReasons": counts_value(&incomplete_reasons),
1093 "budgetTokens": jsrt::json::number(budget_tokens),
1094 "byModel": by_model_list(frame_by_model),
1095 });
1096 let host_call = |name: &str| {
1097 jsrt::json::number(
1098 stats
1099 .as_ref()
1100 .and_then(|s| s.host_calls.get(name).copied())
1101 .unwrap_or(0.0),
1102 )
1103 };
1104 frames_value["calls"] = if stats_cover {
1105 json!({"infer": host_call("rlm.infer"), "map": host_call("rlm.map")})
1106 } else {
1107 Value::Null
1108 };
1109 let guards = stats.as_ref().map_or(Value::Null, |s| {
1110 Value::Object(
1111 s.guards
1112 .iter()
1113 .map(|(name, g)| {
1114 (
1115 name.clone(),
1116 json!({
1117 "checks": jsrt::json::number(g.checks),
1118 "blocked": jsrt::json::number(g.blocked),
1119 "unchecked": jsrt::json::number(g.unchecked),
1120 "afterChecks": jsrt::json::number(g.after_checks),
1121 "afterFindings": jsrt::json::number(g.after_findings),
1122 "ms": jsrt::json::number(g.ms),
1123 }),
1124 )
1125 })
1126 .collect(),
1127 )
1128 });
1129 json!({
1130 "schema": SESSION_REPORT_SCHEMA,
1131 "session": {
1132 "id": log.header.id,
1133 "path": log.path.display().to_string(),
1134 "cwd": log.header.cwd,
1135 "name": name,
1136 "createdAt": jsrt::json::number(log.header.created_at),
1137 "modifiedAt": jsrt::json::number(log.modified_at),
1138 "bytes": jsrt::json::number(log.bytes as f64),
1139 "parentSessionId": log.header.parent_session_id,
1140 },
1141 "mode": mode,
1142 "turns": turns,
1143 "root": {
1144 "models": root_model_list
1145 .into_iter()
1146 .map(|(model, responses)| json!({"model": model, "responses": jsrt::json::number(responses)}))
1147 .collect::<Vec<_>>(),
1148 },
1149 "cells": cells,
1150 "depth": {
1151 "verdict": depth_verdict(
1152 level,
1153 frames[0],
1154 children.len() as f64,
1155 nested_children,
1156 typed.len() as f64,
1157 background.len() as f64,
1158 ),
1159 "level": jsrt::json::number(level),
1160 "frames": frames_value,
1161 "subagents": {
1162 "count": jsrt::json::number(children.len() as f64),
1163 "maxDepth": jsrt::json::number(max_child_depth),
1164 "nested": jsrt::json::number(nested_children),
1165 "completed": jsrt::json::number(state(&children, &["completed"])),
1166 "failed": jsrt::json::number(state(&children, &["failed"])),
1167 "cancelled": jsrt::json::number(state(&children, &["cancelled"])),
1168 "interrupted": jsrt::json::number(state(&children, &["interrupted"])),
1169 "running": jsrt::json::number(state(&children, &["admitted", "running"])),
1170 "byModel": by_model_list(child_by_model),
1171 "verdicts": {
1172 "verified": jsrt::json::number(verdicts[0]),
1173 "contradicted": jsrt::json::number(verdicts[1]),
1174 "unverified": jsrt::json::number(verdicts[2]),
1175 "invalid": jsrt::json::number(verdicts[3]),
1176 "unchecked": jsrt::json::number(verdicts[4]),
1177 "none": jsrt::json::number(verdicts[5]),
1178 },
1179 "claims": {"passed": jsrt::json::number(claims[0]), "failed": jsrt::json::number(claims[1]), "blocked": jsrt::json::number(claims[2])},
1180 "worktrees": worktrees,
1181 },
1182 "workflows": if stats_cover { json!({"runs": host_call("workflows.run")}) } else { Value::Null },
1183 "typedAgents": {"count": jsrt::json::number(typed.len() as f64), "byDefinition": counts_value(&by_definition)},
1184 "backgroundJobs": {
1185 "count": jsrt::json::number(background.len() as f64),
1186 "completed": jsrt::json::number(state(&background, &["completed"])),
1187 "failed": jsrt::json::number(state(&background, &["failed"])),
1188 "cancelled": jsrt::json::number(state(&background, &["cancelled", "interrupted"])),
1189 "running": jsrt::json::number(state(&background, &["admitted", "running"])),
1190 },
1191 },
1192 "usage": {
1193 "lanes": {
1194 "root": buckets.lanes[0].fields(),
1195 "frames": buckets.lanes[1].fields(),
1196 "subagents": buckets.lanes[2].fields(),
1197 "other": buckets.lanes[3].fields(),
1198 },
1199 "models": models
1200 .into_iter()
1201 .map(|(model, bucket)| {
1202 let mut map = Map::new();
1203 map.insert("model".into(), Value::String(model));
1204 map.extend(bucket.fields());
1205 Value::Object(map)
1206 })
1207 .collect::<Vec<_>>(),
1208 "total": buckets.total.fields(),
1209 },
1210 "guardrails": {
1211 "guards": guards,
1212 "secretsMasked": secrets_masked,
1213 "hints": counts_value(&hints),
1214 "nudges": reported_nudges,
1215 "usageLimitBlocks": stats.as_ref().map_or(Value::Null, |s| jsrt::json::number(s.usage_limit_blocks)),
1216 "countersSince": match &stats {
1217 Some(s) if !stats_cover => jsrt::json::number(s.since),
1218 _ => Value::Null,
1219 },
1220 },
1221 "memory": match recall {
1222 Some(recall) => json!({"operations": operations, "recall": recall}),
1223 None => json!({"operations": operations}),
1224 },
1225 "unrecorded": unrecorded,
1226 })
1227}
1228
1229fn counts_value(counts: &IndexMap<String, f64>) -> Value {
1230 Value::Object(
1231 counts
1232 .iter()
1233 .map(|(k, v)| (k.clone(), jsrt::json::number(*v)))
1234 .collect(),
1235 )
1236}