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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Whether a loaded session is settled: no turn is under way, and neither
//! the daemon nor the session's host will start one for work the session
//! waits on. Its clients follow it as [`rho_protocol::SessionEvent::Activity`]
//! (see `feed`), which `wait` of `rho mcp serve` returns on.
//!
//! Settled is not "nothing runs": a session whose prompts hold until their
//! work finishes (`BackgroundWait::HoldPrompt`) is never woken, so it
//! settles once its turn ends, even a cancelled one that left a subagent
//! running. A session the daemon or its host wakes is active while work it
//! waits on (see `awaited`) runs or holds a result no turn took, unless the
//! waker gave up: a delivery turn that ended without taking its result,
//! with nothing left running, is not started again (see `idle_wake`), and a
//! host whose wake watcher could not be armed never wakes the session (see
//! `t3_wake`). A turn starting clears that.
//!
//! The level is reported as the session is added, as each turn starts (by
//! the feed, ahead of the turn's state) and ends, as its background work
//! starts or ends, and as its waker gives up. Each report reads the work
//! under the feed's lock, taking only the work's own locks, never the
//! turn's or the runtime's.
//!
//! A settled session that reports to its starter may never report on its
//! own: [`LoadedSession::stop_unreported`] decides, and may queue a notice
//! in its starter's inbox, whose lock is a leaf.
use std::sync::atomic::Ordering;
use super::{super::jobs::Delivered, LoadedSession, TurnSlot};
impl LoadedSession {
/// Tells the session's clients whether it is settled, if that changed
/// since they were last told; a settled session that owes its starter
/// a report it will never make stops owing it (see
/// [`Self::stop_unreported`]).
pub(super) fn report_activity(&self) {
if self.nothing_may_wake() {
self.stop_unreported();
}
}
/// Whether the session is settled and nothing may wake it, not even
/// news of a pull request it watches: a session that reports to its
/// starter then works no more on what it owes it. Tells the session's
/// clients whether it is settled, if that changed.
pub(super) fn nothing_may_wake(&self) -> bool {
let settled =
self.feed.update_activity(|| self.wake_due()) == rho_protocol::SessionActivity::Settled;
let news_may_wake = || {
self.background
.awaited()
.is_some_and(|work| work.watches_pull_requests())
};
settled && !news_may_wake()
}
/// Nothing may wake the session. Should it report to its starter, with
/// its last turn having answered without a report, it will never report
/// on its own: its job records that it stopped without a report, and
/// its starter is told, now if loaded, else as it loads. The job
/// records the stop only while no turn is under way, under the lock a
/// turn starts under, so a turn that starts meanwhile wins and nothing
/// is told (see `jobs::JobRegistry::stop_unreported`).
fn stop_unreported(&self) {
if self.peer_wake.reports_to().is_none() {
return;
}
self.peer_wake.tell_stopped(
self.feed.name(),
|| self.jobs.stop_unreported(&self.job),
|starter| self.receipt(Delivered::Stopped { starter }),
);
}
/// The daemon will not wake the session for the result its work holds
/// (see `idle_wake`): it settles, and its clients hear so at once.
pub(super) fn give_up_waking(&self) {
self.mark_wake_given_up();
self.report_activity();
}
/// As [`Self::give_up_waking`], for a caller whose turn's end reports
/// the session's activity next, as a T3 watcher that could not be
/// armed does (see `turn_wake`). Call before that end is reported.
pub(super) fn mark_wake_given_up(&self) {
self.wake_gave_up.store(true, Ordering::Relaxed);
}
/// A client asked that the session's results wait for its next turn
/// ([`rho_protocol::SESSION_BACKGROUND_HOLD`]): the daemon starts no
/// turn to deliver them meanwhile, and its background work, running or
/// ended, keeps it neither loaded nor an upgrade waiting (see `unload`)
/// nor from settling, which its clients hear at once.
pub(super) fn hold_wake(&self) {
// Under the turn lock, which a delivery turn reads the hold under
// and every turn clears it under as it starts: a delivery turn
// either never starts, or started already and leaves the hold set
// for its end, so cancelling it delivers nothing either.
let turn = self.turn();
self.hold_wake_under(&turn);
drop(turn);
self.give_up_waking();
}
/// As [`Self::hold_wake`], for a caller that holds the turn lock, its
/// guard's slot passed as proof, and gives up waking the session itself
/// once it lets go.
pub(super) fn hold_wake_under(&self, _turn: &TurnSlot) {
self.wake_held.store(true, Ordering::Relaxed);
}
/// Whether the session's results wait for its next turn (see
/// [`Self::hold_wake`] and [`Self::hold_wake_for_prompt`]).
pub(super) fn wake_held(&self) -> bool {
self.wake_held.load(Ordering::Relaxed) || self.wake_held_for_prompt.load(Ordering::Relaxed)
}
/// The prompt that reopens the session starts its turn next, which
/// takes the session's results at its first boundary, so the daemon
/// starts no turn to deliver them meanwhile, which would leave that
/// prompt `SessionBusy`. Its turn starting clears the hold; a prompt
/// that starts none gives it back ([`Self::release_wake_for_prompt`]).
/// Call before the session's idle watcher starts.
pub(super) fn hold_wake_for_prompt(&self) {
self.wake_held_for_prompt.store(true, Ordering::Relaxed);
}
/// The prompt that reopened the session starts no turn after all: the
/// daemon delivers its results as it would have, if it held them for
/// that prompt.
pub(super) fn release_wake_for_prompt(&self) {
if self.wake_held_for_prompt.swap(false, Ordering::Relaxed) {
if let Some(wake) = &self.idle_wake {
wake.look_again();
}
}
}
/// Whether the daemon or the session's host gave up waking the session
/// for the result its work holds (see [`Self::give_up_waking`]), until
/// a turn starts.
pub(super) fn wake_given_up(&self) -> bool {
self.wake_gave_up.load(Ordering::Relaxed)
}
/// A turn starts and takes the session's results, so whatever gave up
/// waking the session for them, or held them, looks again once it ends.
/// Call under the turn lock as the turn starts, before its activity is
/// reported.
pub(super) fn rearm_wake(&self) {
self.wake_gave_up.store(false, Ordering::Relaxed);
self.wake_held.store(false, Ordering::Relaxed);
self.wake_held_for_prompt.store(false, Ordering::Relaxed);
}
/// Whether the daemon or the session's host will start a turn for work
/// the session waits on.
fn wake_due(&self) -> bool {
!self.wake_given_up()
&& self
.background
.awaited()
.is_some_and(|work| work.outstanding())
}
}
#[cfg(test)]
#[path = "settle_tests.rs"]
mod tests;