1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! The subagent rail of a daemon session: the status of each run the
//! daemon's jobs name (`daemon_jobs`), read from the run's artifact
//! directory, which the daemon writes under the rho home this process
//! shares. `/attach` then opens a run's artifacts from the same directory.
//! A finished run's cost there joins the session's subagent cost, as the
//! in-process subagent manager's does (`app_loop.rs`).
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
};
use rho_protocol::{display::NotificationCard, JobSummary};
use super::{daemon_jobs::ENDED_RETENTION, App};
use crate::{
session::SessionPersistence,
subagent::{self, RunStatus},
tools::agent::SubagentSnapshot,
};
/// What the subagent rail remembers between job lists.
#[derive(Default)]
pub(super) struct SubagentRuns {
/// Each run's artifact directory, resolved once.
directories: HashMap<String, PathBuf>,
/// The finished runs whose result the transcript shows delivered:
/// their rows left the rail with it.
delivered: HashSet<String>,
/// The rows the last job list put on the rail.
shown: Vec<SubagentSnapshot>,
/// The finished runs whose cost the session's subagent cost counts.
costed: HashSet<String>,
}
impl SubagentRuns {
/// The status of `run`, read from its artifact directory.
fn status(&mut self, run: &str) -> Option<RunStatus> {
if !self.directories.contains_key(run) {
let directory = subagent::resolve_run_directory(run).ok()?;
self.directories.insert(run.to_owned(), directory);
}
subagent::read_status(&self.directories[run].join(subagent::RESULT_FILE_NAME))
}
/// What `run`, as `status` has it, adds to the session's subagent cost
/// in USD micros: its cost the first time it is seen finished, else
/// nothing.
fn claim_cost(&mut self, run: &str, status: &RunStatus) -> u64 {
if !status.state.is_terminal() || !self.costed.insert(run.to_owned()) {
return 0;
}
status.total_cost_usd.map_or(0, subagent::usd_to_micros)
}
}
impl App {
/// Puts `jobs`, the followed session's subagent jobs worth showing at
/// `now` (Unix seconds), on the subagent rail. A run whose status
/// cannot be read has no row.
pub(super) fn show_daemon_subagents(&mut self, jobs: &[&JobSummary], now: u64) {
let snapshots: Vec<_> = jobs
.iter()
.filter_map(|job| job.run.clone())
.filter_map(|run| {
if self.daemon_rail.subagents.delivered.contains(&run) {
return None;
}
let runs = &mut self.daemon_rail.subagents;
let status = runs.status(&run)?;
let cost = runs.claim_cost(&run, &status);
self.usage.extra.subagent = self.usage.extra.subagent.saturating_add(cost);
let done = status.state.is_terminal();
let ended_long_ago = status.finished_at.is_some_and(|finished| {
now.saturating_sub(finished) >= ENDED_RETENTION.as_secs()
});
(!done || !ended_long_ago).then(|| SubagentSnapshot {
id: run,
agent_id: status.agent_id.clone().unwrap_or_else(|| "agent".into()),
title: status.title.clone(),
elapsed: status.elapsed_duration(now).unwrap_or_default(),
done,
prior_notices: Vec::new(),
// The rail does not show it.
persistence: SessionPersistence::Saved,
status,
})
})
.collect();
self.daemon_rail.subagents.shown.clone_from(&snapshots);
if self.subagent_panel.ingest(snapshots) {
self.refresh_attach_picker();
}
}
/// The transcript shows `card`, a result or message the model was
/// given. When it names a finished run, that run's result is delivered,
/// so its row leaves the rail now, as an in-process row does, instead
/// of once its retention passed.
pub(super) fn daemon_result_delivered(&mut self, card: &NotificationCard) {
let Some(run) = &card.reference else {
return;
};
let runs = &mut self.daemon_rail.subagents;
// A running subagent's message names its run too; its row stays.
let Some(status) = runs.status(run).filter(|status| status.state.is_terminal()) else {
return;
};
// Its row leaves before a job list may see it finished.
let cost = runs.claim_cost(run, &status);
self.usage.extra.subagent = self.usage.extra.subagent.saturating_add(cost);
runs.delivered.insert(run.clone());
runs.shown.retain(|shown| &shown.id != run);
let shown = runs.shown.clone();
if self.subagent_panel.ingest(shown) {
self.refresh_attach_picker();
}
}
}