rho-coding-agent 0.12.0

A lightweight agent harness inspired by Pi
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
use thiserror::Error;

mod compaction;
mod context_tracker;
mod history;

pub use compaction::CompactionConfig;
pub use history::{HistorySink, SessionHistorySink};

use compaction::{
    build_summary_request_messages, partition_messages_for_compaction,
    replacement_history_from_summary, should_compact,
};
use context_tracker::ContextTracker;

use crate::model::{
    openai::prompt_cache_key_from_session_id, ContentBlock, ContextUsage, DynModelProvider,
    Message, ModelError, ModelEvent, ModelRequest, ModelResponse, ModelUsage,
};
use crate::prompt::system_prompt;
use crate::tool::{truncate, ToolContext, ToolDisplayStyle, ToolError, ToolRegistry, ToolResult};

#[derive(Debug, Error)]
pub enum AgentError {
    #[error("Provider error: {0}")]
    Provider(#[from] ModelError),
    #[error("Tool error: {0}")]
    Tool(#[from] ToolError),
    #[error("Message persistence error: {0}")]
    MessagePersistence(#[from] anyhow::Error),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AgentEvent {
    StepStarted(usize),
    ToolStarted,
    OutputDelta(String),
    ReasoningDelta(String),
    ContextUsage(ContextUsage),
    Usage(ModelUsage),
    ToolFinished {
        name: String,
        command: Option<String>,
        ok: bool,
        content: String,
        display_style: ToolDisplayStyle,
        display_lines: Vec<String>,
    },
}

pub struct Agent {
    provider: DynModelProvider,
    tools: ToolRegistry,
    ctx: ToolContext,
    messages: Vec<Message>,
    history_sink: Option<Box<dyn HistorySink>>,
    prompt_cache_key: Option<String>,
    include_system_prompt: bool,
    compaction: CompactionConfig,
    context_tracker: ContextTracker,
}

impl Agent {
    pub fn new(provider: DynModelProvider, tools: ToolRegistry, ctx: ToolContext) -> Self {
        let messages = initial_messages(&tools, &ctx.cwd, true);
        Self {
            provider,
            tools,
            ctx,
            messages,
            history_sink: None,
            prompt_cache_key: None,
            include_system_prompt: true,
            compaction: CompactionConfig::default(),
            context_tracker: ContextTracker::default(),
        }
    }

    pub fn without_system_prompt(mut self) -> Self {
        self.include_system_prompt = false;
        self.messages = initial_messages(&self.tools, &self.ctx.cwd, self.include_system_prompt);
        self
    }

    pub fn with_history(mut self, history: Vec<Message>) -> Self {
        self.messages.extend(history);
        self
    }

    pub fn replace_history(&mut self, history: Vec<Message>) {
        self.messages = initial_messages(&self.tools, &self.ctx.cwd, self.include_system_prompt);
        self.messages.extend(history);
    }

    pub fn messages(&self) -> &[Message] {
        &self.messages
    }

    pub fn set_history_sink(&mut self, sink: impl HistorySink + 'static) {
        self.history_sink = Some(Box::new(sink));
    }

    pub fn clear_history_sink(&mut self) {
        self.history_sink = None;
    }

    pub fn set_compaction_config(&mut self, compaction: CompactionConfig) {
        self.compaction = compaction;
    }

    pub fn set_context_window(&mut self, context_window: Option<u64>) {
        self.context_tracker.set_configured_window(context_window);
    }

    pub fn set_session_id(&mut self, session_id: Option<String>) {
        self.prompt_cache_key = session_id
            .as_deref()
            .and_then(prompt_cache_key_from_session_id);
    }

    pub fn replace_provider(&mut self, provider: DynModelProvider) {
        self.provider = provider;
        self.context_tracker.replace_provider();
    }

    pub fn reset(&mut self) {
        self.messages = initial_messages(&self.tools, &self.ctx.cwd, self.include_system_prompt);
        self.context_tracker.reset();
    }

    pub async fn run(&mut self, user_prompt: String) -> Result<String, AgentError> {
        self.run_with_events(user_prompt, |_| Ok(())).await
    }

    pub fn load_skill(&mut self, skill: &crate::skills::Skill) -> Result<(), AgentError> {
        self.push_message(Message::user_text(format!(
            "Loaded skill `{}` from {}:\n\n{}",
            skill.name,
            skill.path.display(),
            truncate(skill.contents.clone(), self.ctx.max_output_bytes)
        )))
    }

    fn push_message(&mut self, message: Message) -> Result<(), AgentError> {
        if let Some(sink) = &mut self.history_sink {
            sink.append_message(&message)?;
        }
        self.messages.push(message);
        Ok(())
    }

    pub async fn run_with_events(
        &mut self,
        user_prompt: String,
        on_event: impl FnMut(AgentEvent) -> Result<(), ModelError>,
    ) -> Result<String, AgentError> {
        self.run_with_events_and_steering(user_prompt, on_event, || Ok(None))
            .await
    }

    pub async fn run_with_events_and_steering(
        &mut self,
        user_prompt: String,
        mut on_event: impl FnMut(AgentEvent) -> Result<(), ModelError>,
        mut next_steer: impl FnMut() -> Result<Option<String>, AgentError>,
    ) -> Result<String, AgentError> {
        let specs = self.tools.specs();
        self.push_message(Message::user_text(user_prompt))?;

        let mut step = 1usize;
        loop {
            self.maybe_compact_history(&specs, &mut on_event).await?;
            on_event(AgentEvent::StepStarted(step))?;
            if let Some(context_usage) = self
                .context_tracker
                .before_provider_request(&self.messages, &specs)
            {
                on_event(AgentEvent::ContextUsage(context_usage))?;
            }
            let response = match self
                .provider
                .send_turn_stream(
                    ModelRequest {
                        messages: self.messages.clone(),
                        tools: specs.clone(),
                        prompt_cache_key: self.prompt_cache_key.clone(),
                    },
                    &mut |event| match event {
                        ModelEvent::OutputDelta(text) => on_event(AgentEvent::OutputDelta(text)),
                        ModelEvent::ReasoningDelta(text) => {
                            on_event(AgentEvent::ReasoningDelta(text))
                        }
                        ModelEvent::Usage(usage) => {
                            if let Some(context_usage) =
                                self.context_tracker.record_provider_usage(&usage)
                            {
                                on_event(AgentEvent::ContextUsage(context_usage))?;
                            }
                            on_event(AgentEvent::Usage(usage))
                        }
                    },
                )
                .await
            {
                Ok(response) => response,
                Err(ModelError::Interrupted) => return Err(ModelError::Interrupted.into()),
                Err(err) if should_retry_model_error(&err) => {
                    self.push_message(Message::user_text(format!(
                        "The previous assistant response could not be processed by the client. Error: {err}\n\nPlease continue from the last request. If you attempted a tool call, emit valid tool-call JSON that exactly matches the required schema."
                    )))?;
                    step += 1;
                    continue;
                }
                Err(err) => return Err(err.into()),
            };
            match response {
                ModelResponse::Assistant(blocks) => {
                    let tool_calls: Vec<_> = blocks
                        .iter()
                        .filter_map(|block| match block {
                            ContentBlock::ToolCall(call) => Some(call.clone()),
                            ContentBlock::Text(_) => None,
                        })
                        .collect();
                    if tool_calls.is_empty() {
                        let answer = blocks
                            .into_iter()
                            .filter_map(|block| match block {
                                ContentBlock::Text(text) => Some(text),
                                ContentBlock::ToolCall(_) => None,
                            })
                            .collect::<Vec<_>>()
                            .join("\n");
                        self.push_message(Message::assistant_text(answer.clone()))?;
                        let Some(steer) = next_steer()? else {
                            return Ok(answer);
                        };
                        self.push_message(Message::user_text(steer))?;
                    } else {
                        on_event(AgentEvent::ToolStarted)?;
                        self.push_message(Message::Assistant(blocks))?;
                        let mut tool_events = Vec::new();
                        for call in tool_calls.iter().cloned() {
                            let name = call.name.clone();
                            let (result, display_style, command, event_content, display_lines) =
                                match self.tools.get(&call.name) {
                                    Some(tool) => {
                                        let display_style = tool.display_style();
                                        let command = tool.display_command(&call.arguments);
                                        let event_content =
                                            tool.display_content(&call.arguments, &self.ctx);
                                        let result = match tool
                                            .call(
                                                call.arguments.clone(),
                                                self.ctx.clone(),
                                                call.id.clone(),
                                            )
                                            .await
                                        {
                                            Ok(result) => result,
                                            Err(err) => ToolResult {
                                                id: call.id.clone(),
                                                ok: false,
                                                content: err.to_string(),
                                            },
                                        };
                                        let mut display_lines =
                                            tool.display_lines(&call.arguments, &self.ctx, &result);
                                        if !result.ok
                                            && !display_lines
                                                .iter()
                                                .any(|line| line == &result.content)
                                        {
                                            display_lines.push(result.content.clone());
                                        }
                                        (
                                            result,
                                            display_style,
                                            command,
                                            event_content,
                                            display_lines,
                                        )
                                    }
                                    None => {
                                        let result = ToolResult {
                                            id: call.id.clone(),
                                            ok: false,
                                            content: format!("Unknown tool: {}", call.name),
                                        };
                                        let display_lines =
                                            vec![call.name.clone(), result.content.clone()];
                                        (
                                            result,
                                            ToolDisplayStyle::default_tool(),
                                            None,
                                            None,
                                            display_lines,
                                        )
                                    }
                                };
                            let display_content =
                                event_content.unwrap_or_else(|| result.content.clone());
                            let ok = result.ok;
                            self.push_message(Message::ToolResult(result))?;
                            tool_events.push(AgentEvent::ToolFinished {
                                name,
                                command,
                                ok,
                                content: display_content,
                                display_style,
                                display_lines,
                            });
                        }
                        for event in tool_events {
                            on_event(event)?;
                        }
                    }
                }
            }
            step += 1;
        }
    }

    async fn maybe_compact_history(
        &mut self,
        specs: &[crate::tool::ToolSpec],
        on_event: &mut impl FnMut(AgentEvent) -> Result<(), ModelError>,
    ) -> Result<(), AgentError> {
        let estimate = self
            .context_tracker
            .estimate_for_compaction(&self.messages, specs);
        if !should_compact(&self.compaction, estimate.tokens, estimate.context_window) {
            return Ok(());
        }
        let Some(partition) =
            partition_messages_for_compaction(&self.messages, self.compaction.recent_messages)
        else {
            return Ok(());
        };

        let response = self
            .provider
            .send_turn_stream(
                ModelRequest {
                    messages: build_summary_request_messages(&partition.compacted_messages),
                    tools: Vec::new(),
                    prompt_cache_key: self.prompt_cache_key.clone(),
                },
                &mut |event| match event {
                    ModelEvent::OutputDelta(_) | ModelEvent::ReasoningDelta(_) => Ok(()),
                    ModelEvent::Usage(usage) => on_event(AgentEvent::Usage(usage)),
                },
            )
            .await?;
        let ModelResponse::Assistant(blocks) = response;
        let summary = blocks
            .into_iter()
            .filter_map(|block| match block {
                ContentBlock::Text(text) => Some(text),
                ContentBlock::ToolCall(_) => None,
            })
            .collect::<Vec<_>>()
            .join("\n")
            .trim()
            .to_string();
        if summary.is_empty() {
            return Err(ModelError::InvalidResponse(
                "compaction summary response did not include text".into(),
            )
            .into());
        }

        self.messages = replacement_history_from_summary(partition, summary);
        self.persist_history_replacement()?;
        let context_usage = self.context_tracker.record_compaction();
        on_event(AgentEvent::ContextUsage(context_usage))?;
        Ok(())
    }

    fn persist_history_replacement(&mut self) -> Result<(), AgentError> {
        if let Some(sink) = &mut self.history_sink {
            let first_history_index = self
                .messages
                .iter()
                .position(|message| !matches!(message, Message::System(_)))
                .unwrap_or(self.messages.len());
            sink.replace_history(&self.messages[first_history_index..])?;
        }
        Ok(())
    }
}

fn should_retry_model_error(error: &ModelError) -> bool {
    matches!(error, ModelError::InvalidResponse(_))
}

fn initial_messages(
    tools: &ToolRegistry,
    cwd: &std::path::Path,
    include_system_prompt: bool,
) -> Vec<Message> {
    if include_system_prompt {
        vec![Message::System(system_prompt(&tools.specs(), cwd))]
    } else {
        Vec::new()
    }
}

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