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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Shows a daemon session's [`DaemonView`]s through the TUI's own transcript
//! paths (#1667): history entries and the live tool batch, fed the same
//! view-model events an in-process turn feeds them, and the busy chrome an
//! in-process turn shows, which here follows the job's state. Input
//! requests open and close their prompts through `daemon_answers`.
use std::time::Instant;
use super::{
activity::ActivityPhase,
compaction_display::CompactionUiOutcome,
daemon_events::{DaemonView, Rest, TurnState},
event_adapter::{compact_finished_event, compact_started_event, ViewModelEvent},
program_status::ProgramStatus,
settings_screen::SettingsUpdate,
App, DefaultTerminal, Entry, HerdrState, StreamKind, ToolEntry,
};
use rho_protocol::display::Presentation;
/// A turn of the followed daemon session that a `/compact` prompt started:
/// the turn is the compaction, and shows as one.
#[derive(Default)]
pub(super) struct DaemonCompactTurn {
/// How its compaction ended, as the status line says it; the turn rests
/// on it.
rest_status: Option<&'static str>,
/// Whether its compaction reduced the conversation, once it ended.
compacted: Option<bool>,
}
impl App {
/// Applies what the followed daemon session changed.
pub(super) async fn show_daemon_views(
&mut self,
views: Vec<DaemonView>,
terminal: &mut DefaultTerminal,
) -> anyhow::Result<()> {
for view in views {
match view {
DaemonView::Clear => self.clear_daemon_transcript(),
DaemonView::InputsReset => self.drop_daemon_inputs(),
DaemonView::User(text) => {
self.finish_streams();
self.insert_entry(&Entry::User(text));
}
// A `/compact` turn's only text is its reply, which says
// again what the card shows.
DaemonView::Text { .. } if self.daemon_compact_turn.is_some() => {}
// A running turn's text goes through the stream an
// in-process turn's does, which holds, paces and previews
// it as `output_streaming` says. History, and a turn this
// TUI is not showing busy, is written as it stands.
DaemonView::Text {
kind,
text,
opens,
replayed: false,
} if self.turn.is_provider_turn() => {
if opens {
// A new message starts a new row, after any notice
// held for the last one's end, and from a clean
// stream, as each in-process step does.
self.finish_streams();
self.reset_streams();
}
self.handle_agent_event(
match kind {
StreamKind::Assistant => ViewModelEvent::OutputDelta(text),
StreamKind::Reasoning => ViewModelEvent::ReasoningDelta(text),
},
terminal,
)?;
}
DaemonView::Text {
kind,
text,
opens,
replayed: _,
} => {
// Replayed into a running turn, as a reattach's tail
// or a reopen mid-reply is: what the stream still holds
// came before it, and the turn shows the phase the text
// put it in live.
let streaming = self.turn.is_provider_turn();
if streaming {
self.finish_streams();
self.turn.set_activity_phase(match kind {
StreamKind::Assistant => ActivityPhase::Responding,
StreamKind::Reasoning => ActivityPhase::Thinking,
});
}
match (opens, streaming) {
(true, true) => {
self.reset_streams();
self.open_transcript_row(kind.entry(text));
}
(true, false) => self.open_transcript_row(kind.entry(text)),
(false, true) => self.continue_stream_with_replayed(kind, &text),
(false, false) => self.push_transcript_entry(kind.entry(text)),
}
}
DaemonView::ToolRunning { call, card } => {
self.handle_agent_event(
ViewModelEvent::ToolStarted {
call_id: call,
card,
},
terminal,
)?;
}
DaemonView::ToolFinished { call, presentation } => {
self.handle_agent_event(
ViewModelEvent::ToolFinished {
call_id: call,
presentation,
// Images a call returned stay in its wire
// content, which cards do not show yet.
image_asset: None,
},
terminal,
)?;
}
DaemonView::CompactionStarted { command } => {
if command {
self.begin_daemon_compact_turn();
}
self.handle_agent_event(compact_started_event(), terminal)?;
self.set_status("compacting context");
}
DaemonView::CompactTurnOver => self.daemon_compact_turn = None,
DaemonView::CompactionEnded(outcome) => {
let status = outcome.status_label();
let compacted = matches!(outcome, CompactionUiOutcome::Completed(_));
self.handle_agent_event(compact_finished_event(outcome), terminal)?;
self.set_status(status);
if let Some(turn) = &mut self.daemon_compact_turn {
turn.rest_status = Some(status);
turn.compacted = Some(compacted);
}
}
DaemonView::Plan(list) => {
self.set_todo_list(list);
}
DaemonView::Context {
used_tokens,
window_tokens,
} => {
self.handle_agent_event(
ViewModelEvent::ContextUsage(
rho_sdk::model::ContextUsage::provider_reported(
used_tokens,
window_tokens,
),
),
terminal,
)?;
}
DaemonView::Spend(cost_usd_micros) => {
// The daemon's figure includes the response under way,
// so the local estimate of its output starts over.
self.usage.live_stream.clear();
self.usage.daemon_spend = Some(cost_usd_micros);
self.sync_daemon_main_cost();
}
DaemonView::CacheWarm {
warming,
cost_usd_micros,
} => self.show_daemon_cache_warm(warming, cost_usd_micros),
DaemonView::Title(title) => self.daemon_title = Some(title),
DaemonView::Options(options) => self.show_daemon_options(*options),
DaemonView::McpInventory(result) => self.show_daemon_mcp(result),
DaemonView::ToolSourcesLoaded => self.show_daemon_tool_sources_loaded(),
DaemonView::Report { report, result } => self.show_daemon_report(report, result),
DaemonView::Login(update) => self.show_daemon_login(update, terminal).await?,
DaemonView::Jobs(jobs) => {
if let Ok(jobs) = &jobs {
self.show_jobs_view(jobs, terminal);
}
self.show_daemon_jobs(jobs);
}
DaemonView::Processes { asked, result } => {
self.show_daemon_processes(asked, result);
}
DaemonView::SettingsLoaded(Ok(settings)) => {
self.show_daemon_settings(SettingsUpdate::Loaded(settings));
}
DaemonView::SettingsLoaded(Err(error)) => {
self.show_daemon_settings(SettingsUpdate::Unreadable(error));
}
DaemonView::SettingsChanged(changed) => {
self.show_daemon_settings(SettingsUpdate::Changed(changed));
}
DaemonView::ConfigureFinished => self.finish_daemon_option_request(),
DaemonView::Turn(state) => self.show_daemon_turn(state).await,
DaemonView::SteerApplied(id) => {
// The user row that follows shows it.
let _ = self.take_accepted_steering(std::slice::from_ref(&id));
}
DaemonView::SteerDelivered(id) => self.record_delivered_steering(&id),
DaemonView::InputOpened(request) => {
// The reply so far shows above what it asks.
self.finish_streams();
self.ask_daemon_input(*request).await;
}
DaemonView::InputClosed { id, notice } => {
self.close_daemon_input(id, notice).await;
}
DaemonView::InputHeld(id) => self.daemon_input_held(id),
DaemonView::TurnSince(since) if self.turn.is_busy() => {
self.turn.start_loading_at(since);
}
DaemonView::TurnSince(_) => {}
DaemonView::Requesting if self.turn.is_provider_turn() => {
self.turn
.set_activity_phase(ActivityPhase::WaitingForProvider);
}
DaemonView::Requesting => {}
DaemonView::Retrying(hint) if self.turn.is_provider_turn() => {
self.turn
.set_activity_phase(ActivityPhase::RetryingProvider);
if let Some(hint) = hint {
self.set_status(hint.status_label());
self.turn.set_provider_retry(hint);
}
}
// No turn shows busy to say it retries.
DaemonView::Retrying(_) => {}
DaemonView::Status(text) => self.set_status(text),
// Held until the message under way ends: a
// notice such as a new title does not break into a reply.
DaemonView::Notice(text) => self.insert_entry(&Entry::Notice(text)),
DaemonView::Card(card) => {
// A result reaches the model between its messages, so
// what the stream still holds was said before it.
self.finish_streams();
self.daemon_result_delivered(&card);
self.insert_entry(&Entry::Tool(ToolEntry::new(
Presentation::Notification(card),
/*expanded*/ false,
/*image*/ None,
/*started_at*/ None,
)));
}
DaemonView::Error(text) => {
self.finish_streams();
self.insert_entry(&Entry::Error(text));
}
}
}
Ok(())
}
/// Writes `text`, replayed, as more of the message the running turn's
/// stream of `kind` shows. It goes through that stream, whole and at
/// once, so the stream's fence, wrap, and emitted text account for it
/// when live text continues the message.
fn continue_stream_with_replayed(&mut self, kind: StreamKind, text: &str) {
self.streams.stream_mut(kind).push_delta(text);
if let Some(fragment) = self.streams.stream_mut(kind).finish() {
self.insert_stream_fragment(fragment, kind);
}
}
/// Appends `entry` as a row of its own, even after a row of its kind
/// that it would otherwise extend: a new message starts a new row.
fn open_transcript_row(&mut self, entry: Entry) {
let index = self.history.len();
self.history.lines_mut().invalidate_from(index);
self.history.push(entry);
self.mark_markdown_images_dirty_from(index);
}
/// Shows the session's turn as the job's state says: the busy chrome,
/// program status, and Herdr state an in-process turn shows while it
/// runs, and how it ended once it rests.
async fn show_daemon_turn(&mut self, state: TurnState) {
match state {
TurnState::Running => {
self.begin_daemon_turn();
if self.turn.activity_phase() == ActivityPhase::WaitingForInput {
self.turn.set_activity_phase(ActivityPhase::Starting);
}
self.report_working().await;
}
TurnState::WaitingForInput => {
self.begin_daemon_turn();
self.turn.set_activity_phase(ActivityPhase::WaitingForInput);
self.report_herdr_state(HerdrState::Blocked, Some("waiting for input"))
.await;
}
TurnState::Resting(rest) => self.end_daemon_turn(rest).await,
}
}
/// Starts showing the turn under way as a `/compact`: the card alone,
/// without the command's row, which the daemon sent just before the
/// compaction started.
fn begin_daemon_compact_turn(&mut self) {
self.daemon_compact_turn = Some(DaemonCompactTurn::default());
if matches!(self.history.last(), Some(Entry::User(_))) {
let index = self.history.len() - 1;
self.history.entries_mut().pop();
self.history.invalidate_from(index);
if let Some(start) = self.turn.current_turn_start() {
self.turn.set_current_turn_start(Some(start.min(index)));
}
}
}
/// Empties the transcript for a reset replay. A turn that shows busy
/// keeps its chrome and clock, but its rows are gone: the replay's
/// running state places its start again, as a first attach does.
pub(super) fn clear_daemon_transcript(&mut self) {
// Before the rows go: what the stream held is replayed with them.
self.reset_streams();
self.usage.live_stream.clear();
self.set_history_entries(Vec::new());
self.turn.clear_tool_calls();
self.set_todo_list(None);
self.drop_daemon_inputs();
self.turn.set_current_turn_start(None);
self.daemon_compact_turn = None;
}
/// The busy chrome of a turn that started, unless it shows already. A
/// turn already showing whose rows a reset replay rebuilt starts after
/// them.
fn begin_daemon_turn(&mut self) {
if self.turn.is_provider_turn() {
if self.turn.current_turn_start().is_none() {
self.turn.set_current_turn_start(Some(self.history.len()));
}
return;
}
// After the turn's user row, which the daemon sent first.
self.turn.set_current_turn_start(Some(self.history.len()));
self.set_status("running");
self.begin_provider_turn_ui();
self.turn.set_activity_phase(ActivityPhase::Starting);
self.turn.start_loading();
}
/// Ends the busy chrome as the turn rested `rest`. Cards still running
/// stay in the transcript as interrupted, as a replay of a turn the
/// daemon never closed leaves them; a session already at rest only
/// tidies those.
///
/// Pending work settles as an in-process turn's does: an interrupted
/// turn hands it back to the composer, unless the person's cancel of
/// questions ended it; any other keeps steers it never applied as
/// follow-ups, and a completed one, or a `/compact` whose compaction
/// failed, arms the next follow-up, which the app loop sends once the
/// composer is free.
async fn end_daemon_turn(&mut self, rest: Rest) {
// What the stream still holds of the reply shows before the turn's
// closing rows, and nothing of it outlasts the turn.
self.finish_streams();
self.reset_streams();
let compact_turn = self.daemon_compact_turn.take();
let questions_cancelled = self.take_questions_cancelled_turn();
// The daemon withdrew the turn's own requests before it said the
// turn ended. A prompt still open is a subagent's question, which
// outlives the turn.
for entry in self.turn.interrupted_tool_entries() {
self.insert_entry(&Entry::Tool(entry));
}
self.turn.clear_tool_calls();
self.turn.set_activity_phase(ActivityPhase::default());
if !self.turn.is_provider_turn() {
return;
}
self.daemon_rail.turn_ended();
self.daemon_turn_rested_for_info();
// Read before `stop_loading` clears the turn clock.
let worked = self.turn.elapsed_at(Instant::now());
if matches!(rest, Rest::Interrupted) && !questions_cancelled {
self.restore_pending_work_to_input();
} else {
self.preserve_unapplied_steering_as_follow_ups();
}
self.clear_accepted_steering();
let (status, settled) = match rest {
Rest::Ready => {
self.end_provider_turn_ui();
// A `/compact` turn shows no footer.
if let Some(worked) = worked.filter(|_| compact_turn.is_none()) {
self.attach_turn_worked(worked);
}
let status = if self.pending.has_follow_ups() {
self.start_follow_ups = true;
format!(
"running next queued message ({})",
self.pending.follow_up_len()
)
} else {
"ready".to_owned()
};
(Some(status), ProgramStatus::Done)
}
Rest::Interrupted => {
self.end_busy_ui();
let status = if questions_cancelled {
"questionnaire cancelled"
} else {
"model interrupted"
};
(Some(status.to_owned()), ProgramStatus::Idle)
}
Rest::Failed { error } => {
self.end_busy_ui();
// A failed compaction still sends what waited for it, as a
// prompt "Compact, then send" parked.
if compact_turn.is_some() && self.pending.has_follow_ups() {
self.start_follow_ups = true;
}
(
Some("error".to_owned()),
ProgramStatus::Error { message: error },
)
}
// The error row says why; the turn may run on in the daemon.
Rest::Unfollowed => {
self.end_busy_ui();
(None, ProgramStatus::Idle)
}
};
self.turn.stop_loading();
self.turn.set_current_turn_start(None);
let compacted = compact_turn.as_ref().and_then(|turn| turn.compacted);
// A `/compact` turn rests as its compaction ended.
let compaction_status = compact_turn
.and_then(|turn| turn.rest_status)
.map(str::to_owned);
if let Some(status) = compaction_status.or(status) {
self.set_status(status);
}
self.settle_daemon_compact_handoff(compacted);
self.settled_program_status = settled;
self.notifier.turn_finished();
self.report_resting_herdr_state().await;
}
}
#[cfg(test)]
#[path = "daemon_transcript_tests.rs"]
mod tests;