rpi-cli 0.21.1

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
Documentation
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Bounded resident history, independent of the prompt mailbox.
use super::*;
use rpi_harness::session::types::{
    BranchEntryPage, BranchEntryPageQuery, BranchOverview, SessionStorage,
};
use rpi_harness::session::Session;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Direction {
    Older,
    Newer,
    Latest,
}
impl Direction {
    pub(super) fn parse(args: &str) -> Option<Self> {
        match args.trim() {
            "" | "older" => Some(Self::Older),
            "newer" => Some(Self::Newer),
            "latest" => Some(Self::Latest),
            _ => None,
        }
    }
}

#[derive(Default)]
struct Resident {
    generation: u64,
    storage: Option<Arc<dyn SessionStorage>>,
    // Entries belong to rendered components, not a second retained payload copy.
    page: Option<BranchEntryPage>,
    error: Option<Arc<Container>>,
    // Page-local runtime notices retire with that page, including late callbacks.
    notices: Arc<Container>,
}
#[derive(Default)]
pub(super) struct HistoryUi {
    resident: Mutex<Resident>,
    worker: Mutex<Option<tokio::task::JoinHandle<()>>>,
    pub(super) unsaved: std::sync::atomic::AtomicBool,
    pub(super) active: Arc<Container>,
    pub(super) panels: Arc<Container>,
    pub(super) header: Arc<Container>,
    /// Startup notices must stay before the live transcript slot. If they are
    /// appended to `chat` directly, streamed output (which belongs to `active`)
    /// renders above the notice box.
    pub(super) updates: Arc<Container>,
}
impl HistoryUi {
    pub(super) fn invalidate(&self) {
        if let Some(worker) = self.worker.lock().unwrap().take() {
            worker.abort();
        }
        self.resident.lock().unwrap().generation += 1;
    }
    pub(super) fn reset(&self) {
        self.invalidate();
        let mut resident = self.resident.lock().unwrap();
        resident.generation += 1;
        resident.page = None;
        resident.error = None;
        resident.notices = Arc::new(Container::new());
        resident.storage = None;
        self.unsaved
            .store(false, std::sync::atomic::Ordering::SeqCst);
        self.active.clear();
        self.panels.clear();
        self.header.clear();
        self.updates.clear();
    }
    pub(super) fn bind(&self, harness: &AgentHarness) {
        self.invalidate();
        let mut resident = self.resident.lock().unwrap();
        resident.generation += 1;
        let storage = harness.session().storage();
        if !resident
            .storage
            .as_ref()
            .is_some_and(|bound| Arc::ptr_eq(bound, &storage))
        {
            resident.page = None;
            resident.notices = Arc::new(Container::new());
        }
        resident.storage = Some(storage);
    }
    pub(super) fn is_bound(&self) -> bool {
        self.resident.lock().unwrap().storage.is_some()
    }
    pub(super) fn notice_container(&self, root: &Arc<Container>) -> Arc<Container> {
        let resident = self.resident.lock().unwrap();
        if resident.storage.is_none() {
            root.clone()
        } else if resident
            .page
            .as_ref()
            .is_some_and(|page| page.has_more_after)
        {
            resident.notices.clone()
        } else {
            self.active.clone()
        }
    }
    pub(super) fn is_latest(&self) -> bool {
        self.resident
            .lock()
            .unwrap()
            .page
            .as_ref()
            .is_none_or(|page| !page.has_more_after)
    }
    fn mount_header(&self, chat: &Arc<Container>) {
        let header: Arc<dyn Component> = self.header.clone();
        if self.header.child_count() > 0
            && !chat
                .get_children()
                .iter()
                .any(|child| Arc::ptr_eq(child, &header))
        {
            chat.add_child(header);
        }
    }
    // Reserve this slot even while empty: the background update check may
    // complete after assistant streaming has already begun.
    pub(super) fn mount_updates(&self, chat: &Arc<Container>) {
        let updates: Arc<dyn Component> = self.updates.clone();
        if !chat
            .get_children()
            .iter()
            .any(|child| Arc::ptr_eq(child, &updates))
        {
            chat.add_child(updates);
        }
    }
    pub(super) fn mount_active(&self, chat: &Arc<Container>) {
        self.mount_header(chat);
        self.mount_updates(chat);
        let active: Arc<dyn Component> = self.active.clone();
        if !chat
            .get_children()
            .iter()
            .any(|child| Arc::ptr_eq(child, &active))
        {
            chat.add_child(active);
        }
        let panels: Arc<dyn Component> = self.panels.clone();
        if !chat
            .get_children()
            .iter()
            .any(|child| Arc::ptr_eq(child, &panels))
        {
            chat.add_child(panels);
        }
    }
}

pub(super) fn apply_overview(footer: &FooterComponent, overview: &BranchOverview) {
    let mut totals = rpi_tui::footer::UsageTotals::default();
    totals.add(
        overview.input,
        overview.output,
        overview.cache_read,
        overview.cache_write,
        overview.cost_total,
    );
    footer.set_usage(totals);
    footer.set_cache_hit_rate(overview.cache_hit_rate);
    footer.set_context_tokens(overview.context_tokens);
}

/// Initial/session-switch replay. Storage is pinned before any asynchronous read.
pub(super) async fn restore(harness: &AgentHarness, state: &Arc<TuiState>, chat: &Arc<Container>) {
    state.history_page.bind(harness);
    restore_bound(state, chat).await;
}

async fn restore_bound(state: &Arc<TuiState>, chat: &Arc<Container>) {
    // Even an empty or unreadable session must have a visible live-operation slot.
    state.history_page.mount_header(chat);
    state.history_page.mount_active(chat);
    let (generation, storage) = {
        let resident = state.history_page.resident.lock().unwrap();
        (resident.generation, resident.storage.clone().unwrap())
    };
    let tree = Session::new(storage.clone(), None).view("main");
    let Ok(expected_leaf) = tree.get_leaf_id().await else {
        return;
    };
    let Ok(page) = tree.branch_entry_page(&Default::default()).await else {
        return;
    };
    let panels = Arc::new(Container::new());
    restore_history_panels(&tree, &panels).await;
    let overview = tree.branch_overview().await.ok();
    let name = tree.get_name().await.ok().flatten();
    if page.leaf_id != expected_leaf || tree.get_leaf_id().await.ok() != Some(expected_leaf) {
        return;
    }
    let mut resident = state.history_page.resident.lock().unwrap();
    if !current(&resident, generation, &storage) {
        return;
    }
    let page_chat = render_page(state, &page.entries);
    resident.notices = Arc::new(Container::new());
    page_chat.add_child(resident.notices.clone());
    clear_settled_active(state);
    chat.clear();
    state.history_page.mount_header(chat);
    state.history_page.mount_updates(chat);
    chat.add_child(page_chat);
    resident.error = None;
    state.history_page.panels.clear();
    for child in panels.get_children() {
        state.history_page.panels.add_child(child);
    }
    state.history_page.mount_active(chat);
    state.footer.set_session_name(name.as_deref());
    if let Some(overview) = overview {
        apply_overview(&state.footer, &overview);
    }
    resident.page = Some(metadata(page));
    state.search.activate();
    state.search.deactivate();
    state.search_bar.set_visible(false);
    if let Some(tui) = &state.tui {
        if let Some(scroll) = tui.get_primary_scroll_view() {
            scroll.invalidate();
        }
        tui.clear_selection();
        tui.request_render(false);
    }
}

fn clear_settled_active(state: &TuiState) {
    // Keep a new prompt reservation out until the previous active slot is retired.
    let status = state.status.lock().unwrap();
    if *status == RunStatus::Idle && !state.user_bash_running() {
        state.history_page.active.clear();
        state.current_assistant.lock().unwrap().take();
        state.tool_components.lock().unwrap().clear();
        state.bash_components.lock().unwrap().clear();
    }
}

fn metadata(mut page: BranchEntryPage) -> BranchEntryPage {
    page.entries.clear();
    page
}
fn current(resident: &Resident, generation: u64, storage: &Arc<dyn SessionStorage>) -> bool {
    resident.generation == generation
        && resident
            .storage
            .as_ref()
            .is_some_and(|bound| Arc::ptr_eq(bound, storage))
}
fn render_page(state: &TuiState, entries: &[Entry]) -> Arc<Container> {
    let chat = Arc::new(Container::new());
    render_history_entries(
        entries.to_vec(),
        &chat,
        state.markdown_transformer(),
        Some(state.extension_session.clone()),
        state.images_visible(),
    );
    state.apply_history_tool_expansion(&chat);
    chat
}

/// A task per request, never sent through TuiMessage: that mailbox awaits a
/// complete provider run. Last request wins; session/operation changes advance
/// the same generation before mutating their backing state.
pub(super) fn request(state: Arc<TuiState>, tui: Arc<TuiAltScreen>, direction: Direction) {
    // A failed write may leave the screen as the only copy: preserve it.
    if state
        .history_page
        .unsaved
        .load(std::sync::atomic::Ordering::SeqCst)
    {
        return;
    }
    let mut worker = state.history_page.worker.lock().unwrap();
    if let Some(previous) = worker.take() {
        previous.abort();
    }
    let (generation, storage, previous) = {
        let mut resident = state.history_page.resident.lock().unwrap();
        let Some(storage) = resident.storage.clone() else {
            return;
        };
        resident.generation += 1;
        (resident.generation, storage, resident.page.clone())
    };
    let task_state = state.clone();
    *worker = Some(tokio::spawn(async move {
        let state = task_state;
        let tree = Session::new(storage.clone(), None).view("main");
        let Ok(expected_leaf) = tree.get_leaf_id().await else {
            return;
        };
        let result = async {
            let previous = match previous {
                Some(page) => page,
                None => tree.branch_entry_page(&Default::default()).await?,
            };
            let query = match direction {
                Direction::Latest => BranchEntryPageQuery::default(),
                Direction::Older if previous.has_more_before => BranchEntryPageQuery {
                    leaf_id: previous.leaf_id,
                    before_id: previous.next_before_id,
                    ..Default::default()
                },
                Direction::Newer if previous.has_more_after => BranchEntryPageQuery {
                    leaf_id: previous.leaf_id,
                    after_id: previous.next_after_id,
                    ..Default::default()
                },
                _ => return Ok(None),
            };
            tree.branch_entry_page(&query).await.map(Some)
        }
        .await;
        // A branch move or appended operation invalidates the captured snapshot.
        if tree.get_leaf_id().await.ok() != Some(expected_leaf) {
            return;
        }
        let mut resident = state.history_page.resident.lock().unwrap();
        if !current(&resident, generation, &storage)
            || state
                .history_page
                .unsaved
                .load(std::sync::atomic::Ordering::SeqCst)
        {
            return;
        }
        match result {
            Ok(Some(page)) => {
                let page_chat = render_page(&state, &page.entries);
                resident.notices = Arc::new(Container::new());
                page_chat.add_child(resident.notices.clone());
                clear_settled_active(&state);
                resident.error = None;
                // Drop every reference to the old page (including image components).
                state.chat_container.clear();
                state.history_page.mount_header(&state.chat_container);
                state.history_page.mount_updates(&state.chat_container);
                state.chat_container.add_child(page_chat);
                if !page.has_more_after {
                    state
                        .chat_container
                        .add_child(state.history_page.active.clone());
                }
                state
                    .chat_container
                    .add_child(state.history_page.panels.clone());
                if let Some(scroll) = tui.get_primary_scroll_view() {
                    scroll.invalidate();
                }
                tui.clear_selection();
                state.search.activate(); // clears query/matches referencing old rows
                state.search.deactivate();
                state.search_bar.set_visible(false);
                resident.page = Some(metadata(page));
                match direction {
                    Direction::Older => tui.scroll_to_bottom(),
                    Direction::Newer => tui.scroll_to_top(),
                    Direction::Latest => tui.scroll_to_bottom(),
                }
                tui.request_render(false); // invalidate scroll/render/image upload caches
            }
            Ok(None) => {}
            Err(error) => {
                // Failure keeps the original page and component references intact.
                if let Some(previous) = resident.error.take() {
                    let previous: Arc<dyn Component> = previous;
                    state.chat_container.remove_child(&previous);
                }
                let notice = Arc::new(Container::new());
                add_error_message(&notice, &format!("Could not load history: {error}"));
                state.chat_container.add_child(notice.clone());
                resident.error = Some(notice);
                tui.request_render(false);
            }
        }
    }));
}

/// Only operation settlement reloads latest, never individual token events.
pub(super) fn settle(state: &Arc<TuiState>, tui: &Arc<TuiAltScreen>) {
    // A failed write may leave the screen as the only copy: preserve it.
    if state
        .history_page
        .unsaved
        .load(std::sync::atomic::Ordering::SeqCst)
    {
        return;
    }
    if state.history_page.is_latest() {
        request(state.clone(), tui.clone(), Direction::Latest);
    } else {
        state.history_page.invalidate();
        clear_settled_active(state);
    }
}

pub(super) fn handle_key(
    state: &Arc<TuiState>,
    tui: &Arc<TuiAltScreen>,
    bindings: &rpi_tui::Keybindings,
    key: &KeyEvent,
) -> bool {
    // Never steal editor navigation or selector input.
    if !state.editor.get_text().is_empty()
        || state.selector_open()
        || state.search.is_active()
        || state.extension_dialog_open()
    {
        return false;
    }
    let Some(scroll) = tui.get_primary_scroll_view() else {
        return false;
    };
    let direction = if bindings.matches(key, rpi_tui::keybindings::keys::BOTTOM)
        && !state.history_page.is_latest()
    {
        Some(Direction::Latest)
    } else if bindings.matches(key, rpi_tui::keybindings::keys::PAGE_UP) && scroll.scroll_top() == 0
    {
        state
            .history_page
            .resident
            .lock()
            .unwrap()
            .page
            .as_ref()
            .filter(|page| page.has_more_before)
            .map(|_| Direction::Older)
    } else if bindings.matches(key, rpi_tui::keybindings::keys::PAGE_DOWN)
        && scroll.scroll_top().saturating_add(scroll.viewport_height()) >= scroll.content_height()
    {
        state
            .history_page
            .resident
            .lock()
            .unwrap()
            .page
            .as_ref()
            .filter(|page| page.has_more_after)
            .map(|_| Direction::Newer)
    } else {
        None
    };
    if let Some(direction) = direction {
        request(state.clone(), tui.clone(), direction);
        true
    } else {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn history_arguments_are_explicit_and_default_to_older() {
        for (arg, expected) in [
            ("", Direction::Older),
            (" older ", Direction::Older),
            ("newer", Direction::Newer),
            ("latest", Direction::Latest),
        ] {
            assert_eq!(Direction::parse(arg), Some(expected));
        }
        for invalid in ["all", "older newer", "latest extra"] {
            assert_eq!(Direction::parse(invalid), None);
        }
    }
}

#[cfg(test)]
#[path = "history/tests.rs"]
mod paging_tests;