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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! `/context` and `/hooks` against a daemon session: the daemon reads the
//! report from the session it owns, and the overlay opens when it answers.
//! `/hooks` first asks the daemon to reload the session's hooks; reading a
//! report alone never changes the session.
//!
//! An answer opens its overlay only while the person still waits on it:
//! [`App::awaited_daemon_report`] names the report asked for last, and a
//! prompt or a switch to another session stops the wait, so an answer that
//! arrives after the person moved on is dropped.
//!
//! `/info` reads its Compaction section the same way, into the overlay it
//! already opened: no one waits on that answer, so it changes no status and
//! is dropped once the overlay closed. The daemon refuses the read during a
//! turn, so [`InfoCompactionRead`] asks again when the turn ends.
//!
//! The provider-context report a resumed session is asked for opens the
//! handoff dialog (`context_handoff::daemon`), while the person has not
//! moved on, by the same rule.
use rho_protocol::SessionReport;
use super::{daemon_events::ReportRefusal, App, ComposerMode, Entry, PanelOverlay};
/// Where the open `/info` overlay's read of a daemon session's Compaction
/// section stands. Each step returns the next state and whether to ask the
/// daemon now.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum InfoCompactionRead {
/// Nothing waits for the section.
#[default]
Idle,
/// Asked. `turn_rested` once a turn ended since: a refusal as busy is
/// then of that turn, and the read can go again at once.
Asked { turn_rested: bool },
/// The session is in a turn: asked when it ends.
AwaitingRest,
}
impl InfoCompactionRead {
fn opened(in_turn: bool) -> (Self, bool) {
if in_turn {
(Self::AwaitingRest, false)
} else {
(Self::Asked { turn_rested: false }, true)
}
}
fn turn_rested(self) -> (Self, bool) {
match self {
Self::Idle => (Self::Idle, false),
Self::Asked { .. } => (Self::Asked { turn_rested: true }, false),
Self::AwaitingRest => (Self::Asked { turn_rested: false }, true),
}
}
fn refused_as_busy(self) -> (Self, bool) {
match self {
Self::Asked { turn_rested: true } => (Self::Asked { turn_rested: false }, true),
Self::Asked { turn_rested: false } | Self::AwaitingRest => (Self::AwaitingRest, false),
Self::Idle => (Self::Idle, false),
}
}
}
/// What the person asked to see, for the status line and a refusal.
fn name(report: SessionReport) -> &'static str {
match report {
SessionReport::Context => "context",
SessionReport::Hooks => "hooks",
SessionReport::Compaction => "compaction",
SessionReport::ProviderContext => "provider context",
// `SessionReport` is non-exhaustive; the TUI asks only for the above.
_ => "report",
}
}
impl App {
/// Asks the daemon for `report` on the followed session.
pub(super) fn request_daemon_report(&mut self, report: SessionReport) {
let asked = self.info.session.daemon.inspect(report);
self.await_daemon_report(report, asked);
}
/// Asks the daemon to reload the followed session's hooks, then for its
/// hooks report, for `/hooks`.
pub(super) fn reload_daemon_hooks(&mut self) {
let asked = self.info.session.daemon.reload_hooks();
self.await_daemon_report(SessionReport::Hooks, asked);
}
/// `/hooks` could not reload the hooks; the report that follows shows
/// the hooks the session keeps. Dropped once the person moved on.
pub(super) fn show_hooks_not_reloaded(&mut self, reason: &str) {
if self.awaited_daemon_report == Some(SessionReport::Hooks) {
self.insert_entry(&Entry::Error(format!("could not reload hooks: {reason}")));
}
}
fn await_daemon_report(&mut self, report: SessionReport, asked: anyhow::Result<()>) {
match asked {
Ok(()) => {
self.awaited_daemon_report = Some(report);
self.set_status_quiet(format!("reading {}", name(report)));
}
Err(error) => self.insert_entry(&Entry::Error(format!(
"could not read {}: {error}",
name(report)
))),
}
}
/// Reads the Compaction section of the `/info` overlay just opened: now,
/// or once the turn under way ends.
pub(super) fn open_daemon_info_compaction(&mut self) {
// Nothing to ask a session not opened yet, which says so rather
// than leave the section out.
if self.info.session.daemon.is_draft() {
self.apply_info_compaction(Err("the session opens with its first prompt".to_owned()));
return;
}
let step = InfoCompactionRead::opened(self.daemon_turn_runs());
self.step_info_compaction(step);
}
/// A turn of the daemon session ended: a read that waited for it goes.
pub(super) fn daemon_turn_rested_for_info(&mut self) {
let step = self.info_compaction_read.turn_rested();
self.step_info_compaction(step);
}
fn step_info_compaction(&mut self, (next, ask): (InfoCompactionRead, bool)) {
// An overlay that closed, however it did, waits for nothing.
if !matches!(
self.input_ui.composer(),
ComposerMode::Panel(PanelOverlay::Info(_))
) {
self.info_compaction_read = InfoCompactionRead::Idle;
return;
}
self.info_compaction_read = next;
if !ask {
return;
}
if let Err(error) = self.info.session.daemon.inspect(SessionReport::Compaction) {
self.info_compaction_read = InfoCompactionRead::Idle;
self.apply_info_compaction(Err(error.to_string()));
}
}
fn show_daemon_info_compaction(&mut self, result: Result<serde_json::Value, ReportRefusal>) {
match result {
Err(refusal) if refusal.busy => {
let (next, ask) = self.info_compaction_read.refused_as_busy();
// Until the read goes again the section says why it waits:
// a job other than the session's turn may hold the session,
// and no turn of this session need ever end.
if !ask {
self.apply_info_compaction(Err(refusal.message));
}
self.step_info_compaction((next, ask));
}
result => {
self.info_compaction_read = InfoCompactionRead::Idle;
self.apply_info_compaction(result.map_err(|refusal| refusal.message).and_then(
|value| serde_json::from_value(value).map_err(|error| error.to_string()),
));
}
}
}
/// Opens the overlay for a report the daemon answered with.
pub(super) fn show_daemon_report(
&mut self,
report: SessionReport,
result: Result<serde_json::Value, ReportRefusal>,
) {
match report {
SessionReport::Compaction => return self.show_daemon_info_compaction(result),
SessionReport::ProviderContext => return self.show_daemon_provider_context(result),
_ => {}
}
let result = result.map_err(|refusal| refusal.message);
// An answer to a report the person asked for before another, or
// before moving on, is not what they wait on.
if self.awaited_daemon_report != Some(report) {
return;
}
self.awaited_daemon_report = None;
// A late answer must not replace what the person opened meanwhile.
if result.is_ok() && !matches!(self.input_ui.composer(), ComposerMode::Input) {
return;
}
let shown = result.and_then(|value| match report {
SessionReport::Context => serde_json::from_value(value)
.map(|report| self.show_context_report(report))
.map_err(|error| error.to_string()),
SessionReport::Hooks => serde_json::from_value(value)
.map(|report| self.show_hooks_report(report))
.map_err(|error| error.to_string()),
_ => Err("this rho does not know the report".to_owned()),
});
if let Err(error) = shown {
self.insert_entry(&Entry::Error(format!(
"could not read {}: {error}",
name(report)
)));
self.set_status_quiet(format!("could not read {}", name(report)));
}
}
}
#[cfg(test)]
#[path = "daemon_reports_tests.rs"]
mod tests;