rho-coding-agent 3.1.1

A fast Rust agent harness with a small footprint and opinionated defaults
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
use std::{
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, Mutex,
    },
    time::Duration,
};

use pretty_assertions::assert_eq;
use rho_sdk::{
    model::{
        AssistantMessage, ContentBlock, Message, MessageTime, ModelEvent, ModelIdentity,
        ModelRequest, ModelResponse, ProviderContextBlock, ReplayScope, ToolCall, ToolSpec,
    },
    provider::{
        ModelProvider, ProviderFuture, ProviderTurnContext, ScriptedProvider, ScriptedTurn,
    },
    tool::{ScriptedTool, ScriptedToolOutcome, Tool, ToolOutput},
    CancellationToken, Rho, SessionOptions, SessionRequestPrefix, SystemPrompt, UserInput,
    Workspace,
};
use serde_json::json;
use tokio::{
    sync::{mpsc, Notify},
    time::timeout,
};

use crate::{
    app::{
        policy::AppPolicy,
        runtime_builder::{build_runtime_with_max_steps, RuntimeBuildOptions},
        session_runtime::{
            test_support::unattended_runtime_on, QuestionSupport, SessionRuntime, Turn, TurnIo,
            TurnOutcome,
        },
    },
    compaction::CompactionConfig,
    config::Config,
    permission::PermissionMode,
    tools::sdk_registry::{AppToolSet, StaticToolBundle},
};

/// Bounds each wait on an explicit signal; never the way a test syncs.
const TEST_BUDGET: Duration = Duration::from_secs(5);

// Covers: a run that crosses its compaction threshold mid-turn compacts
// automatically and still finishes with the model's reply.
// Owner: session runtime turn (automatic compaction within a run)
#[tokio::test]
async fn a_run_that_crosses_its_compaction_threshold_compacts_and_finishes() {
    let provider = ScriptedProvider::new(
        ModelIdentity::new("test", "test", "turn-compaction"),
        [
            ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::ToolCall(
                ToolCall::new("call-1".into(), "expand_context".into(), json!({})),
            )])),
            ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
                "compact summary".into(),
            )])),
            ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
                "done".into(),
            )])),
        ],
    );
    let tools: Vec<Arc<dyn Tool>> = vec![Arc::new(ScriptedTool::new(
        ToolSpec {
            name: "expand_context".into(),
            description: "return a large result".into(),
            input_schema: json!({"type": "object"}),
        },
        ScriptedToolOutcome::Success(ToolOutput::text("tool context ".repeat(500))),
    ))];
    let mut inventory = AppToolSet::disabled();
    inventory.add_bundle(StaticToolBundle::new(tools));
    let root = tempfile::tempdir().unwrap();
    let config = Config::default();
    let shared: Arc<dyn ModelProvider> = Arc::new(provider.clone());
    let sdk = agent_runtime(
        Arc::clone(&shared),
        &inventory,
        root.path(),
        CompactionConfig {
            auto_compact: true,
            threshold_percent: 5,
            target_percent: 1,
            summarizer: None,
        },
    );
    assert_eq!(sdk.diagnostics().compaction_trigger_tokens(), Some(50));
    let mut runtime = unattended_runtime_on(
        root.path(),
        &config,
        QuestionSupport::Unavailable,
        sdk,
        shared,
    )
    .await;
    let prompt = runtime.begin_prompt(&config).await.unwrap();
    let input = UserInput::text("go");
    let turn = Turn {
        display: input.clone(),
        model: input,
        calls: Vec::new(),
    };
    // Nothing reads the events, and the control sender stays open so the
    // turn has a host.
    let (events, _shown) = mpsc::unbounded_channel();
    let (_controls, control) = mpsc::unbounded_channel();

    let ended = timeout(
        TEST_BUDGET,
        runtime.run_turn(&prompt, turn, TurnIo { events, control }, &config),
    )
    .await
    .expect("the turn ends")
    .unwrap();

    let reply = match ended {
        TurnOutcome::Completed(outcome) => outcome.text().to_owned(),
        TurnOutcome::Cancelled => panic!("the turn was cancelled"),
    };
    let requests = provider.recorded_requests();
    let summary = requests[2]
        .messages
        .iter()
        .find_map(|message| match message {
            Message::CompactionSummary(summary) => Some(summary.trigger),
            _ => None,
        })
        .expect("compacted history carries a typed summary");
    assert_eq!(
        (reply, requests.len(), summary),
        (
            "done".to_owned(),
            3,
            Some(rho_sdk::CompactionTrigger::Automatic)
        )
    );
    drop(prompt);
    runtime.shutdown().await;
}

/// The SDK runtime the host builds for a root agent on `provider` with
/// `tools`, working in `root`.
fn agent_runtime(
    provider: Arc<dyn ModelProvider>,
    tools: &AppToolSet,
    root: &std::path::Path,
    compaction: CompactionConfig,
) -> Rho {
    build_runtime_with_max_steps(
        RuntimeBuildOptions {
            provider,
            tools,
            workspace: Workspace::new(root).unwrap(),
            workspace_policy: AppPolicy::for_mode(
                PermissionMode::Auto,
                Default::default(),
                /*search_path*/ None,
            ),
            approval_session: None,
            system_prompt: SystemPrompt::None,
            reasoning: rho_sdk::ReasoningLevel::Off,
            service_tier: None,
            compaction,
            context_window: Some(1_000),
            usage_purpose: "agent",
            usage_parent_session_id: None,
            usage_recording: Default::default(),
            hook_host_labels: rho_sdk::hooks::HookHostLabels::new(),
            hooks: None,
            diagnostics: crate::diagnostics::test_diagnostics("test", "test"),
            recall: None,
        },
        /*max_steps*/ None,
    )
    .unwrap()
}

/// Compaction that never runs, so requests carry history as it was saved.
fn no_compaction() -> CompactionConfig {
    CompactionConfig {
        auto_compact: false,
        threshold_percent: 80,
        target_percent: 40,
        summarizer: None,
    }
}

/// Runs one prompt of `text` to completion on `runtime`.
async fn run_prompt(runtime: &mut SessionRuntime, config: &Config, text: &str) {
    let prompt = runtime.begin_prompt(config).await.unwrap();
    let input = UserInput::text(text);
    let turn = Turn {
        display: input.clone(),
        model: input,
        calls: Vec::new(),
    };
    let (events, _shown) = mpsc::unbounded_channel();
    let (_controls, control) = mpsc::unbounded_channel();
    let ended = timeout(
        TEST_BUDGET,
        runtime.run_turn(&prompt, turn, TurnIo { events, control }, config),
    )
    .await
    .expect("the turn ends")
    .unwrap();
    assert!(matches!(ended, TurnOutcome::Completed(_)), "{ended:?}");
}

/// The exact bytes `prefix` would send as messages.
fn request_bytes(prefix: &SessionRequestPrefix) -> String {
    serde_json::to_string(prefix.as_request(CancellationToken::new()).messages).unwrap()
}

// Covers: a provider request stamps each user message with only when it was
// sent, and sends the reply before it, thinking included, exactly as the model
// wrote it; a session resumed from its file
// sends the same bytes as before, so the prompt cache survives a reload.
// Owner: session runtime turn (message timestamps)
#[tokio::test]
async fn requests_carry_saved_message_times_across_resume() {
    let identity = ModelIdentity::new("test", "test", "timestamps");
    let thinking = json!({"type": "thinking", "thinking": "weighing it", "signature": "sig"});
    let provider = ScriptedProvider::new(
        identity.clone(),
        [
            ScriptedTurn::streaming(
                vec![ModelEvent::ProviderContext {
                    kind: "anthropic_content_block".into(),
                    position: Some(0),
                    data: thinking.clone(),
                    replay: ReplayScope::ExactModel,
                }],
                ModelResponse::Assistant(vec![ContentBlock::Text("first answer".into())]),
            ),
            ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
                "second answer".into(),
            )])),
        ],
    );
    let root = tempfile::tempdir().unwrap();
    let config = Config::default();
    let shared: Arc<dyn ModelProvider> = Arc::new(provider.clone());
    let tools = AppToolSet::disabled();
    let sdk = agent_runtime(Arc::clone(&shared), &tools, root.path(), no_compaction());
    let mut runtime = unattended_runtime_on(
        root.path(),
        &config,
        QuestionSupport::Unavailable,
        sdk,
        Arc::clone(&shared),
    )
    .await;

    // Every stamp is one of these, a minute apart at most.
    let before = MessageTime::now();
    run_prompt(&mut runtime, &config, "hello").await;
    run_prompt(&mut runtime, &config, "again").await;
    let window = [before, MessageTime::now()];

    let requests = provider.recorded_requests();
    let sent = &requests[1].messages;
    assert_eq!(
        sent,
        &vec![
            stamped_user(sent, &window, "hello"),
            answer_as_written(&identity, &thinking),
            stamped_user(sent, &window, "again"),
        ]
    );

    let live = runtime.built.session.request_prefix().unwrap();
    let stored = runtime.storage.file().expect("the session is saved");
    let snapshot = stored
        .snapshot_for_resume(identity, "resume".into())
        .unwrap();
    let resumed = agent_runtime(shared, &tools, root.path(), no_compaction())
        .session(SessionOptions::from_snapshot(snapshot))
        .await
        .unwrap();
    assert_eq!(
        request_bytes(&resumed.request_prefix().unwrap()),
        request_bytes(&live)
    );
    runtime.shutdown().await;
}

/// The first answer as the model wrote it, thinking included.
fn answer_as_written(identity: &ModelIdentity, thinking: &serde_json::Value) -> Message {
    Message::assistant(AssistantMessage {
        content: vec![ContentBlock::Text("first answer".into())],
        provenance: Some(identity.clone()),
        reasoning_summary: None,
        provider_context: vec![ProviderContextBlock {
            identity: identity.clone(),
            kind: "anthropic_content_block".into(),
            position: Some(0),
            data: thinking.clone(),
            replay: ReplayScope::ExactModel,
        }],
    })
}

/// The user message `text` in `sent`, stamped with only the time it was
/// sent, at either end of `window`.
fn stamped_user(sent: &[Message], window: &[MessageTime; 2], text: &str) -> Message {
    window
        .iter()
        .map(|time| Message::user_text(format!("{time}\n{text}")))
        .find(|message| sent.contains(message))
        .unwrap_or_else(|| panic!("no stamped {text:?} in {sent:#?}"))
}

/// A provider whose first turn streams `partial`, then holds until its run is
/// cancelled; later turns answer `done`. Records every request's messages.
#[derive(Default)]
struct CutOffFirstReply {
    streamed: Notify,
    calls: AtomicUsize,
    requests: Mutex<Vec<Vec<Message>>>,
}

impl ModelProvider for CutOffFirstReply {
    fn identity(&self) -> ModelIdentity {
        ModelIdentity::new("test", "test", "cut-off")
    }

    fn send_turn<'a>(&'a self, _request: ModelRequest<'a>) -> ProviderFuture<'a> {
        Box::pin(async {
            Ok(ModelResponse::Assistant(vec![ContentBlock::Text(
                "done".into(),
            )]))
        })
    }

    fn send_turn_stream<'a>(
        &'a self,
        request: ModelRequest<'a>,
        context: ProviderTurnContext,
    ) -> ProviderFuture<'a> {
        Box::pin(async move {
            self.requests
                .lock()
                .unwrap()
                .push(request.messages.to_vec());
            if self.calls.fetch_add(1, Ordering::AcqRel) > 0 {
                return self.send_turn(request).await;
            }
            context
                .events
                .send(ModelEvent::OutputDelta("partial".into()))
                .await?;
            self.streamed.notify_one();
            request.cancellation.cancelled().await;
            Err(rho_sdk::ProviderError::interrupted("cancelled"))
        })
    }
}

// Covers: a reply cut off by an interrupt goes back to the provider as the
// model wrote it, and the next user message's stamp carries only when that
// message was sent.
// Owner: session runtime turn (message timestamps)
#[tokio::test]
async fn the_stamp_after_an_interrupted_reply_is_only_its_own_time() {
    let provider = Arc::new(CutOffFirstReply::default());
    let root = tempfile::tempdir().unwrap();
    let config = Config::default();
    let shared: Arc<dyn ModelProvider> = provider.clone();
    let sdk = agent_runtime(
        Arc::clone(&shared),
        &AppToolSet::disabled(),
        root.path(),
        no_compaction(),
    );
    let mut runtime = unattended_runtime_on(
        root.path(),
        &config,
        QuestionSupport::Unavailable,
        sdk,
        shared,
    )
    .await;
    let handle = runtime.handle();

    let before = MessageTime::now();
    let prompt = runtime.begin_prompt(&config).await.unwrap();
    let input = UserInput::text("hello");
    let turn = Turn {
        display: input.clone(),
        model: input,
        calls: Vec::new(),
    };
    let (events, _shown) = mpsc::unbounded_channel();
    let (_controls, control) = mpsc::unbounded_channel();
    let interrupt = async {
        provider.streamed.notified().await;
        handle.cancel();
    };
    let (ended, ()) = timeout(TEST_BUDGET, async {
        tokio::join!(
            runtime.run_turn(&prompt, turn, TurnIo { events, control }, &config),
            interrupt
        )
    })
    .await
    .expect("the turn ends");
    assert!(
        matches!(ended, Ok(TurnOutcome::Cancelled)),
        "{:?}",
        ended.map(|_| ())
    );
    drop(prompt);
    run_prompt(&mut runtime, &config, "next").await;
    let window = [before, MessageTime::now()];

    let sent = provider.requests.lock().unwrap()[1].clone();
    let Message::AbortedAssistant(cut_off) = &sent[1] else {
        panic!("no cut-off reply in {sent:#?}");
    };
    assert_eq!(
        (
            sent[0].clone(),
            cut_off.content.clone(),
            sent[2].clone(),
            sent.len()
        ),
        (
            stamped_user(&sent, &window, "hello"),
            vec![ContentBlock::Text("partial".into())],
            stamped_user(&sent, &window, "next"),
            3
        )
    );
    runtime.shutdown().await;
}