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
120
121
122
123
124
125
126
127
128
129
130
131
132
//! The subagent rows of a daemon session's agent rail: 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.
//! The sessions the session started have rows too (`daemon_started`).
//! A finished run's cost there joins the session's subagent cost, as the
//! in-process subagent manager's does (`app_loop.rs`).
//!
//! A run followed up after it finished runs again under the same id, as a
//! new job writing the same artifacts. Its status no longer being terminal
//! says a new attempt began, so what the rail remembered of the last one,
//! its delivery and its cost, no longer holds.
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 agent 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 agent rail. A run whose status cannot be
/// read has no row.
pub(super) fn show_daemon_subagents(&mut self, jobs: &[&JobSummary], now: u64) {
// One row per run: an ended job and the job following it up read
// the same artifacts.
let mut listed = HashSet::new();
let snapshots: Vec<_> = jobs
.iter()
.filter_map(|job| job.run.clone())
.filter(|run| listed.insert(run.clone()))
.filter_map(|run| {
let runs = &mut self.daemon_rail.subagents;
let status = runs.status(&run)?;
if !status.state.is_terminal() {
runs.delivered.remove(&run);
runs.costed.remove(&run);
} else if runs.delivered.contains(&run) {
return None;
}
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.agent_rail.ingest_runs(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.agent_rail.ingest_runs(shown) {
self.refresh_attach_picker();
}
}
}