rho-coding-agent 3.4.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Rebuild a client's view of a session from its saved display rows,
//! without running tools.

use std::{
    collections::{HashMap, VecDeque},
    path::{Path, PathBuf},
    sync::Arc,
};

use rho_protocol::{
    display::Presentation, MessageId, PlanEntry, SessionEvent as WireEvent, ToolCallId,
    ToolCallUpdate, ToolStatus,
};
use rho_sdk::{
    model::{self, Message, SemanticMessage, ToolResult},
    tool::ToolMetadata,
};

use super::{
    cards, encoded_len, opened_call, plan_entries, result_content, status_update, user_content,
    CallIds, UNNAMED_TOOL_TITLE,
};
use crate::{
    app::{interactive_presenter::InteractiveToolPresenter, session_runtime::SessionTools},
    display_transcript::DisplayTranscript,
    session::tree::{DisplayRowOrigin, ProjectedMessage},
    tools::{
        mcp::exported_name::McpToolNames,
        ui::{SurfaceContent, SurfaceRequest},
    },
};

/// Display rows a session saved together, such as one turn's, kept to
/// replay to a client that attaches with a reset ([`SavedTranscript`]).
///
/// The rows share their messages with the session's history. The events
/// they replay as would not: each tool output appears in them twice, as the
/// call's content and in its card. So the turn keeps the rows, and with
/// them what replay could not derive from the rows again, as it was when
/// they saved: each call's wire id and start metadata, the session's MCP
/// names and its plan. A replay renders the same events from them every
/// time, a row at a time as its client takes them.
///
/// Each saved message replays whole: one event per message, never chunks.
/// Message ids derive from the row a message was saved in, and tool call
/// ids from the provider's saved ids, numbered after the rows saved before.
/// A call the history left without a result, as a killed process does,
/// closes `failed`, since history is not running work. The turn's replay
/// ends with the session's plan as it stood, when it had one.
///
/// Calls carry the cards Rho's TUI draws for them
/// (`ToolCallUpdate::display`): a call opens with the card live draws at its
/// start, from the saved call alone (`InteractiveToolPresenter::for_saved_calls`),
/// and a result brings the card the TUI restores from history. The call an
/// aborted response was still writing gets none: the wire replays no
/// partial calls.
#[derive(Debug)]
pub(crate) struct SavedTurn {
    rows: Vec<ProjectedMessage>,
    /// Session working directory; titles show paths relative to it.
    workspace: PathBuf,
    /// The tools' MCP names: they identify MCP calls, whose capped names
    /// cannot be decoded.
    mcp_tools: McpToolNames,
    /// Each call the rows open, in the order replay opens them.
    calls: Vec<OpenedCall>,
    plan: Vec<PlanEntry>,
    /// Where in the turn's events the ones JSON cannot hold are, such as one
    /// with a non-UTF-8 path, which no client is sent: its connection would
    /// fail on it. Ascending.
    unencodable: Vec<usize>,
    /// How many events a client is sent.
    sent: usize,
}

/// A call replay opened: its wire id, and what its tool reported at start.
/// Saved calls keep no metadata, so each tool reported what it would at
/// start, as it does live; a result whose call history lost has none.
#[derive(Clone, Debug)]
struct OpenedCall {
    id: ToolCallId,
    start: ToolMetadata,
    /// The A2UI surface a `ui` call showed. Saved calls keep no surface
    /// messages either: the session's surfaces show the call again from
    /// its arguments, which is how they come back after a reload. Only a
    /// call whose saved result says it showed its surface is shown again
    /// ([`crate::tools::ui::showed`]): one that was refused, found no `ui`
    /// tool or never ran showed the user nothing, though its arguments may
    /// be a block the session's surfaces would take now.
    surface: Option<Arc<SurfaceContent>>,
}

impl SavedTurn {
    /// `rows`, a saved session's display rows (`SavedHistory::display_rows`),
    /// as `tools` describe them now. Numbers their calls after those `ids`
    /// has counted.
    pub(super) fn capture(
        rows: &[ProjectedMessage],
        workspace: &Path,
        tools: SessionTools<'_>,
        ids: &mut CallIds,
    ) -> Self {
        let mcp_tools = tools.mcp_report().tool_names();
        let plan = plan_entries(&tools.todo_state());
        let mut showed: HashMap<&str, VecDeque<bool>> = HashMap::new();
        for row in rows {
            if let Message::ToolResult(result) = &row.message {
                showed
                    .entry(result.id.as_str())
                    .or_default()
                    .push_back(crate::tools::ui::showed(result));
            }
        }
        let mut calls = Capture {
            tools,
            ids,
            showed,
            opened: Vec::new(),
        };
        // Rendering once, a row at a time, records the calls and finds the
        // events no client can be sent, without holding the events.
        let mut replay = Replay::new(workspace, &mcp_tools);
        let mut unencodable = Vec::new();
        let mut rendered = 0;
        let mut check = |events: Vec<WireEvent>| {
            for event in events {
                if encoded_len(&event).is_none() {
                    unencodable.push(rendered);
                }
                rendered += 1;
            }
        };
        for row in rows {
            replay.row(row, &mut calls);
            check(replay.take_events());
        }
        replay.finish(&plan);
        check(replay.take_events());
        Self {
            rows: rows.to_vec(),
            workspace: workspace.to_owned(),
            mcp_tools,
            calls: calls.opened,
            plan,
            sent: rendered - unencodable.len(),
            unencodable,
        }
    }
}

/// The events a client is sent to replay one [`SavedTurn`], rendered a row
/// at a time as it takes them.
struct TurnEvents {
    turn: Arc<SavedTurn>,
    replay: Replay,
    /// The next row to render.
    row: usize,
    /// The next of the turn's calls to open.
    call: usize,
    finished: bool,
    /// Rendered and not taken yet.
    pending: VecDeque<WireEvent>,
    /// How many events were rendered before `pending`'s first.
    rendered: usize,
}

impl TurnEvents {
    fn new(turn: Arc<SavedTurn>) -> Self {
        Self {
            replay: Replay::new(&turn.workspace, &turn.mcp_tools),
            turn,
            row: 0,
            call: 0,
            finished: false,
            pending: VecDeque::new(),
            rendered: 0,
        }
    }
}

impl Iterator for TurnEvents {
    type Item = WireEvent;

    fn next(&mut self) -> Option<WireEvent> {
        loop {
            if let Some(event) = self.pending.pop_front() {
                let at = self.rendered;
                self.rendered += 1;
                if self.turn.unencodable.binary_search(&at).is_err() {
                    return Some(event);
                }
                continue;
            }
            let mut calls = Recorded {
                calls: &self.turn.calls,
                next: &mut self.call,
            };
            match self.turn.rows.get(self.row) {
                Some(row) => {
                    self.replay.row(row, &mut calls);
                    self.row += 1;
                }
                None if !self.finished => {
                    self.replay.finish(&self.turn.plan);
                    self.finished = true;
                }
                None => {
                    // Rendering from the rows alone, the replay opens the
                    // calls capture recorded and sends what it counted.
                    debug_assert_eq!(self.call, self.turn.calls.len());
                    debug_assert_eq!(self.rendered, self.turn.sent + self.turn.unencodable.len());
                    return None;
                }
            }
            self.pending.extend(self.replay.take_events());
        }
    }
}

/// The turns a session saved, in order, as a client that resets replays
/// them.
#[derive(Debug, Default)]
pub(crate) struct SavedTranscript {
    /// Shared with the replays under way, which render them as their
    /// clients take the events.
    turns: Vec<Arc<SavedTurn>>,
    /// How many events a replay sends.
    sent: usize,
}

impl SavedTranscript {
    pub(crate) fn push(&mut self, turn: SavedTurn) {
        self.sent += turn.sent;
        self.turns.push(Arc::new(turn));
    }

    /// How many events [`Self::events`] yields.
    pub(crate) fn len(&self) -> usize {
        self.sent
    }

    /// The wire events that replay every turn, in order, the same each
    /// time. They render a row at a time as the replay is taken, so a reset
    /// never holds a long session's events at once.
    pub(crate) fn events(&self) -> impl Iterator<Item = WireEvent> + Send + Sync + 'static {
        self.turns.clone().into_iter().flat_map(TurnEvents::new)
    }
}

/// Where replay gets the wire id and start metadata of each call it opens.
trait CallSource {
    /// The next call's: `call`, opened under saved id `saved`, or `None`
    /// for a result whose call history lost.
    fn open(&mut self, saved: &str, call: Option<&model::ToolCall>) -> OpenedCall;
}

/// Opens calls as a turn saves: numbered after the session's earlier calls,
/// with what the session's tools report for them now, recorded.
struct Capture<'a> {
    tools: SessionTools<'a>,
    ids: &'a mut CallIds,
    /// Whether each saved result under an id says its call showed a
    /// surface, in the rows' order: the answer for the calls opened under
    /// the id, in theirs. A call the rows hold no result for showed none.
    showed: HashMap<&'a str, VecDeque<bool>>,
    opened: Vec<OpenedCall>,
}

impl CallSource for Capture<'_> {
    fn open(&mut self, saved: &str, call: Option<&model::ToolCall>) -> OpenedCall {
        let start = call
            .and_then(|call| {
                let tool = self.tools.tool_named(&call.name)?;
                Some(tool.start_metadata(&call.arguments))
            })
            .unwrap_or_default();
        let showed = call.is_some_and(|_| {
            self.showed
                .get_mut(saved)
                .and_then(VecDeque::pop_front)
                .unwrap_or(false)
        });
        let surface = call
            .filter(|call| showed && call.name == crate::tools::ui::TOOL_NAME)
            .and_then(|call| {
                let request = SurfaceRequest::of_arguments(&call.arguments)?;
                self.tools.surfaces()?.show(saved, &request).ok()
            });
        let opened = OpenedCall {
            id: self.ids.open(saved),
            start,
            surface,
        };
        self.opened.push(opened.clone());
        opened
    }
}

/// Opens the calls a [`Capture`] recorded, in order.
struct Recorded<'a> {
    calls: &'a [OpenedCall],
    next: &'a mut usize,
}

impl CallSource for Recorded<'_> {
    fn open(&mut self, _saved: &str, _call: Option<&model::ToolCall>) -> OpenedCall {
        let opened = self
            .calls
            .get(*self.next)
            .cloned()
            .expect("a saved turn renders the calls its capture opened");
        *self.next += 1;
        opened
    }
}

/// The id replay gives the first message of the display row saved at
/// `origin`: a live user message saved there takes it too, so a client sees
/// one message whether it watched the prompt live or got it replayed.
pub(super) fn saved_message_id(origin: &DisplayRowOrigin) -> MessageId {
    let mut ids = MessageIds::default();
    ids.enter(Some(origin));
    ids.next()
}

/// A replay of display rows under way: what it needs from the rows it
/// rendered to render the next.
struct Replay {
    /// Session working directory; titles show paths relative to it.
    workspace: PathBuf,
    /// The session's MCP tool names.
    mcp_tools: McpToolNames,
    /// Draws calls as the TUI draws them, for `ToolCallUpdate::display`.
    presenter: InteractiveToolPresenter,
    /// The latest call opened under each saved id, for its results.
    calls: HashMap<String, ReplayedCall>,
    /// How many calls replay opened.
    opened: usize,
    message_ids: MessageIds,
    /// Rendered since the last [`Self::take_events`].
    events: Vec<WireEvent>,
}

/// A call replay opened, and what it shows so far.
struct ReplayedCall {
    id: ToolCallId,
    /// The saved call, or a stand-in for an orphan result's, which its
    /// results' cards are drawn from.
    call: model::ToolCall,
    status: ToolStatus,
    /// Its results so far, shared with the rows: a later result's content
    /// includes theirs.
    results: Vec<Arc<ToolResult>>,
    /// How many calls replay opened before it, for closing calls in order.
    opened_at: usize,
    /// The A2UI surface it showed, which every update of the call repeats.
    surface: Option<Arc<SurfaceContent>>,
}

impl ReplayedCall {
    fn closed(self) -> WireEvent {
        WireEvent::tool_call(status_update(self.id, Some(ToolStatus::Failed)))
    }

    /// The call's content: every result's, in order, then the surface it
    /// showed.
    fn content(&self) -> Vec<rho_protocol::ToolContent> {
        self.results
            .iter()
            .flat_map(|result| result_content(&result.content, &result.images))
            .chain(self.surface.as_deref().map(SurfaceContent::wire))
            .collect()
    }
}

impl Replay {
    fn new(workspace: &Path, mcp_tools: &McpToolNames) -> Self {
        Self {
            workspace: workspace.to_owned(),
            mcp_tools: mcp_tools.clone(),
            presenter: InteractiveToolPresenter::for_saved_calls(
                workspace.to_owned(),
                mcp_tools.clone(),
            ),
            calls: HashMap::new(),
            opened: 0,
            message_ids: MessageIds::default(),
            events: Vec::new(),
        }
    }

    /// Renders `row`, opening its calls from `calls`.
    fn row(&mut self, row: &ProjectedMessage, calls: &mut dyn CallSource) {
        self.message_ids.enter(row.origin.as_ref());
        self.message(&row.message, calls);
    }

    /// Ends the replay: closes the calls the rows left open, in the order
    /// they opened, then sends `plan` when it has entries.
    fn finish(&mut self, plan: &[PlanEntry]) {
        let mut unfinished: Vec<_> = std::mem::take(&mut self.calls)
            .into_values()
            .filter(|call| call.status == ToolStatus::InProgress)
            .collect();
        unfinished.sort_by_key(|call| call.opened_at);
        self.events
            .extend(unfinished.into_iter().map(ReplayedCall::closed));
        if !plan.is_empty() {
            self.events.push(WireEvent::Plan {
                entries: plan.to_vec(),
            });
        }
    }

    fn take_events(&mut self) -> Vec<WireEvent> {
        std::mem::take(&mut self.events)
    }

    fn message(&mut self, message: &Message, calls: &mut dyn CallSource) {
        // A result is kept, shared, until its call's last result.
        if let Message::ToolResult(result) = message {
            self.result(result, calls);
            return;
        }
        match message.semantic() {
            SemanticMessage::ToolResult(_) => unreachable!("matched above"),
            SemanticMessage::User(blocks) | SemanticMessage::BoundaryInput(blocks) => {
                let content = user_content(blocks);
                if !content.is_empty() {
                    let message = self.message_ids.next();
                    self.events.push(WireEvent::UserMessage {
                        message,
                        content,
                        steer: None,
                    });
                }
            }
            SemanticMessage::Assistant(blocks) => self.agent(blocks, calls),
            SemanticMessage::EnrichedAssistant(assistant) => {
                // Reasoning streams before the text and calls it led to, so
                // it replays first too.
                if let Some(summary) = assistant
                    .reasoning_summary
                    .as_deref()
                    .filter(|summary| !summary.is_empty())
                {
                    let message = self.message_ids.next();
                    self.events.push(WireEvent::ThoughtChunk {
                        message,
                        text: summary.to_owned(),
                    });
                }
                self.agent(&assistant.content, calls);
            }
            SemanticMessage::AbortedAssistant(assistant) => self.agent(&assistant.content, calls),
            SemanticMessage::System(text) => {
                if let Some(transcript) = DisplayTranscript::from_display(text) {
                    self.events.extend(cards(&transcript));
                }
            }
            // Display rows never hold model-only entries.
            SemanticMessage::CompactionSummary(_)
            | SemanticMessage::ModelContext(_)
            | SemanticMessage::ReasoningShift(_) => {}
        }
    }

    /// An assistant message: its text, as one message per run of text
    /// between tool calls (live text after a call is a new message too), and
    /// its calls. The wire's agent messages carry text only, so images an
    /// assistant message holds are not replayed.
    fn agent(&mut self, blocks: &[model::ContentBlock], calls: &mut dyn CallSource) {
        let mut text = String::new();
        for block in blocks {
            match block {
                model::ContentBlock::Text(part) => text.push_str(part),
                model::ContentBlock::Image(_) => {}
                model::ContentBlock::ToolCall(call) => {
                    self.agent_text(std::mem::take(&mut text));
                    self.call(call, calls);
                }
            }
        }
        self.agent_text(text);
    }

    fn agent_text(&mut self, text: String) {
        if text.is_empty() {
            return;
        }
        let message = self.message_ids.next();
        self.events
            .push(WireEvent::AgentMessageChunk { message, text });
    }

    fn call(&mut self, call: &model::ToolCall, calls: &mut dyn CallSource) {
        let OpenedCall {
            id,
            start: metadata,
            surface,
        } = calls.open(&call.id, Some(call));
        // Drawn as live draws the call's start; an empty saved id cannot
        // start, so its proposal's card stands.
        let proposed = self.presenter.proposed(call.clone()).card;
        let card = rho_sdk::ToolCallId::from_string(call.id.as_str()).map_or(proposed, |call_id| {
            self.presenter
                .started(call_id, call.name.clone(), metadata.clone())
                .card
        });
        // Its result's card is drawn from the saved call.
        self.presenter.forget(&call.id);
        let opened = ToolCallUpdate {
            display: Some(Presentation::Card(card)),
            // A call that waited showed its surface before it had a result.
            content: surface
                .as_deref()
                .map(|surface| vec![SurfaceContent::wire(surface)]),
            ..opened_call(
                id.clone(),
                &call.name,
                Some(call.arguments.clone()),
                &metadata,
                &self.workspace,
                &self.mcp_tools,
            )
        };
        self.open(&call.id, id, call.clone(), Vec::new(), surface, opened);
    }

    /// A result updates the latest call opened under its id; results replace
    /// a call's content as a whole, so it keeps earlier output. An orphan
    /// result still opens a complete card.
    fn result(&mut self, result: &Arc<ToolResult>, calls: &mut dyn CallSource) {
        let ok = !result.is_failure();
        let status = if ok {
            ToolStatus::Completed
        } else {
            ToolStatus::Failed
        };
        if let Some(call) = self.calls.get_mut(&result.id) {
            call.status = status;
            call.results.push(Arc::clone(result));
            let shown = self.presenter.historical(&call.call, ok, &result.content);
            self.events.push(WireEvent::tool_call(ToolCallUpdate {
                content: Some(call.content()),
                display: Some(shown.presentation),
                ..status_update(call.id.clone(), Some(status))
            }));
            return;
        }
        let OpenedCall { id, .. } = calls.open(&result.id, None);
        // Drawn as the TUI draws a result whose call history lost.
        let call = model::ToolCall::new(
            result.id.clone(),
            "tool".into(),
            serde_json::Value::Object(Default::default()),
        );
        let shown = self.presenter.historical(&call, ok, &result.content);
        let orphan = ToolCallUpdate {
            title: Some(UNNAMED_TOOL_TITLE.to_owned()),
            kind: Some(rho_protocol::ToolKind::Other),
            content: Some(result_content(&result.content, &result.images)),
            display: Some(shown.presentation),
            ..status_update(id.clone(), Some(status))
        };
        self.open(
            &result.id,
            id,
            call,
            vec![Arc::clone(result)],
            /*surface*/ None,
            orphan,
        );
    }

    /// Sends `opened`, the card of `call` opened as `id` under `saved` with
    /// `results` so far, first closing a call still open under the same saved id, which it
    /// supersedes.
    fn open(
        &mut self,
        saved: &str,
        id: ToolCallId,
        call: model::ToolCall,
        results: Vec<Arc<ToolResult>>,
        surface: Option<Arc<SurfaceContent>>,
        opened: ToolCallUpdate,
    ) {
        let call = ReplayedCall {
            id,
            call,
            status: opened.status.unwrap_or(ToolStatus::InProgress),
            results,
            opened_at: self.opened,
            surface,
        };
        self.opened += 1;
        if let Some(superseded) = self.calls.insert(saved.to_owned(), call) {
            if superseded.status == ToolStatus::InProgress {
                self.events.push(superseded.closed());
            }
        }
        self.events.push(WireEvent::tool_call(opened));
    }
}

/// Ids for replayed messages that stay the same each time a session loads.
///
/// A message is named after the display row it was saved in, so an id is
/// unique to one insertion, even of text saved twice. One saved row can
/// replay as several messages (text after a tool call, a reasoning
/// summary), which are numbered in order.
#[derive(Default)]
struct MessageIds {
    saved: String,
    part: u32,
}

impl MessageIds {
    /// Moves on to the row saved at `origin`. A row the load added has none
    /// and names no message of its own, so it continues the previous row.
    fn enter(&mut self, origin: Option<&DisplayRowOrigin>) {
        if let Some(DisplayRowOrigin { node, row }) = origin {
            self.saved = format!("{node}-{row}");
            self.part = 0;
        }
    }

    /// The id of the next message the current saved row replays as.
    fn next(&mut self) -> MessageId {
        let id = MessageId::new(format!("saved-{}-{}", self.saved, self.part));
        self.part += 1;
        id
    }
}

#[cfg(test)]
#[path = "replay_tests.rs"]
mod tests;