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
133
134
135
136
137
138
139
140
141
142
143
144
//! What the agent of another runtime that runs a session is doing with the
//! tools rho serves it, which two readers act on: the agent budget, which
//! lends a turn's slot only while the agent waits on those tools alone (see
//! `budget`), and the background delivery, which sends nothing into a turn
//! while they have the agent's attention (see `acp_background`). It counts,
//! as the served tools and the budget tell it (see `acp_tools` and
//! `budget`), and holds the two predicates its readers act on:
//! [`Activity::waits_on_served_tools`] for the budget, and
//! [`Activity::served_tools_busy`] for the delivery.
use std::sync::Arc;
use tokio::sync::watch;
/// The session's agent activity, as it changes. One per session, whatever
/// turn its calls came from: a call can outlive its turn.
#[derive(Clone)]
pub(super) struct AgentActivity(Arc<watch::Sender<Activity>>);
/// One look at the agent's activity.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Activity {
/// The served calls running, a call the bridge parked included
/// ([`AgentActivity::call_running`]).
calls: usize,
/// The agent's requests to the served tools whose answers are not yet
/// ready: the agent is blocked on each
/// ([`AgentActivity::request_outstanding`]).
requests: usize,
/// The agent's requests to the served tools whose answers have yet to
/// go back: from the request until its answer is readied, or given up
/// on, a wait for the turn's slot included, which `requests` no longer
/// counts ([`AgentActivity::answer_pending`]).
answers: usize,
/// The agent's own tool calls in progress, outside the served tools, as
/// its run last told ([`AgentActivity::set_agent_calls`]).
agent_calls: usize,
}
impl Activity {
/// Whether the agent waits on the served tools alone: blocked on a
/// request to them, so it makes no model request until it has the
/// answer, with no tool call of its own in progress that could, as a
/// Claude Code Task subagent makes model requests while its agent
/// waits.
pub(super) fn waits_on_served_tools(self) -> bool {
self.requests > 0 && self.agent_calls == 0
}
/// Whether the served tools have the agent's attention: a call runs, a
/// parked one included, or a request's answer has yet to go back, as
/// while it waits for the turn's slot.
pub(super) fn served_tools_busy(self) -> bool {
self.calls > 0 || self.answers > 0
}
}
impl Default for AgentActivity {
fn default() -> Self {
Self(Arc::new(watch::Sender::new(Activity::default())))
}
}
impl AgentActivity {
/// Counts a served call running until the guard it returns drops.
pub(super) fn call_running(&self) -> CallRunning {
self.0.send_modify(|activity| activity.calls += 1);
CallRunning(self.clone())
}
/// Counts one of the agent's requests to the served tools outstanding
/// until the guard it returns drops.
pub(super) fn request_outstanding(&self) -> RequestOutstanding {
self.0.send_modify(|activity| activity.requests += 1);
RequestOutstanding(self.clone())
}
/// Counts one of the agent's requests to the served tools whose answer
/// has yet to go back until the guard it returns drops.
pub(super) fn answer_pending(&self) -> AnswerPending {
self.0.send_modify(|activity| activity.answers += 1);
AnswerPending(self.clone())
}
/// The agent's own tool calls in progress, outside the served tools,
/// now number `running`.
pub(super) fn set_agent_calls(&self, running: usize) {
self.0.send_if_modified(|activity| {
let changed = activity.agent_calls != running;
activity.agent_calls = running;
changed
});
}
/// The activity now.
pub(super) fn now(&self) -> Activity {
*self.0.borrow()
}
/// Follows the activity as it changes. The receiver never closes while
/// `self` lives.
pub(super) fn watch(&self) -> watch::Receiver<Activity> {
self.0.subscribe()
}
/// Resolves once `done` holds of the activity: at once when it does.
pub(super) async fn until(&self, done: impl FnMut(&Activity) -> bool) {
let mut activity = self.0.subscribe();
// `self` holds the sender, so the wait ends only once `done` holds.
let _ = activity.wait_for(done).await;
}
}
/// A served call [`AgentActivity`] counts running, until it drops.
pub(super) struct CallRunning(AgentActivity);
impl Drop for CallRunning {
fn drop(&mut self) {
let Self(AgentActivity(activity)) = self;
activity.send_modify(|activity| activity.calls -= 1);
}
}
/// A request of the agent's [`AgentActivity`] counts outstanding, until it
/// drops.
pub(super) struct RequestOutstanding(AgentActivity);
impl Drop for RequestOutstanding {
fn drop(&mut self) {
let Self(AgentActivity(activity)) = self;
activity.send_modify(|activity| activity.requests -= 1);
}
}
/// A request of the agent's whose answer [`AgentActivity`] counts as yet to
/// go back, until it drops.
pub(super) struct AnswerPending(AgentActivity);
impl Drop for AnswerPending {
fn drop(&mut self) {
let Self(AgentActivity(activity)) = self;
activity.send_modify(|activity| activity.answers -= 1);
}
}