rho-coding-agent 2.20.0

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
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
use pretty_assertions::assert_eq;
use rho_sdk::{
    model::{ContentBlock, Message, ModelResponse, ToolCall},
    provider::{ScriptedProvider, ScriptedTurn},
    SessionOptions, UserInput,
};
use serde_json::json;

use super::*;
use crate::{
    agent::{AgentCapabilities, ToolCapability},
    tools::{sdk_registry::ToolSetOptions, todo::TodoList},
};

fn checklist(content: &str) -> TodoList {
    TodoList::parse(json!({"todos": [{"content": content, "status": "in_progress"}]})).unwrap()
}

fn call(id: &str, name: &str, arguments: serde_json::Value) -> ScriptedTurn {
    ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::ToolCall(
        ToolCall {
            id: id.into(),
            name: name.into(),
            arguments,
        },
    )]))
}

fn text(text: &str) -> ScriptedTurn {
    ScriptedTurn::completed(ModelResponse::Assistant(vec![ContentBlock::Text(
        text.into(),
    )]))
}

async fn todo_runtime(
    turns: Vec<ScriptedTurn>,
    storage: Option<StoredSession>,
) -> (InteractiveRuntime, ScriptedProvider) {
    let mut interactive = test_runtime(Vec::new()).await;
    let provider = ScriptedProvider::new(interactive.provider_identity(), turns);
    interactive.provider =
        ProviderController::new(Arc::new(provider.clone()), rho_sdk::ReasoningLevel::Off);
    interactive.compaction.auto_compact = false;
    interactive.tools = AppToolSet::new(
        &interactive.config,
        interactive.diagnostics.clone(),
        ToolSetOptions::new(AgentCapabilities::new(
            [ToolCapability::Todo].into_iter().collect(),
        )),
    );
    let options = storage
        .as_ref()
        .map_or_else(SessionOptions::new, |storage| {
            SessionOptions::new().id(SessionId::from_string(storage.id()).unwrap())
        });
    let session = interactive.runtime.session(options).await.unwrap();
    let snapshot = session.snapshot();
    interactive.sessions = InteractiveSessionController::new(
        session,
        storage,
        interactive.tools.web_access().clone(),
        /*recall*/ None,
        /*advisor*/ None,
    )
    .with_todo_state(interactive.tools.todo_state());
    interactive
        .rebuild_session(
            snapshot,
            ReplacementLifecycle::Rebound,
            SessionWriteRetention::Keep,
            PromptTransition::Keep,
        )
        .await
        .unwrap();
    (interactive, provider)
}

// Covers: rejected replacements, unprinted codemode calls, and a script failure
// after a successful nested todo must not lose or misreport the accepted list.
// Owner: interactive host tool execution and checklist state.
#[tokio::test]
async fn todo_tracks_successful_direct_and_nested_replacements_only() {
    let first = checklist("first\nexact whitespace  ");
    let nested = checklist("nested replacement");
    let (mut interactive, provider) = todo_runtime(
        vec![
            call("first", "todo", serde_json::to_value(&first).unwrap()),
            text("first accepted"),
            call(
                "invalid",
                "todo",
                json!({"todos": [{"content": "", "status": "pending"}]}),
            ),
            text("invalid rejected"),
            call(
                "nested",
                "codemode",
                json!({"script": format!(
                    "call_tool(\"todo\", {})\nfail(\"after successful nested update\")",
                    serde_json::to_string(&nested).unwrap(),
                )}),
            ),
            text("nested accepted despite script failure"),
            call(
                "clear",
                "codemode",
                json!({"script": "call_tool(\"todo\", {\"todos\": []})"}),
            ),
            text("done"),
        ],
        None,
    )
    .await;
    assert_eq!(interactive.todo_list(), None);
    for expected in [first.clone(), first, nested, TodoList { todos: Vec::new() }] {
        interactive
            .start(UserInput::text("track tasks"), None)
            .await
            .unwrap();
        interactive.finish_run().await.unwrap();
        assert_eq!(interactive.todo_list(), Some(expected));
        let requests = provider.recorded_requests();
        let request = requests.last().unwrap();
        assert_eq!(
            request.messages.last(),
            rho_sdk::RequestContext::messages(
                &interactive.tools.todo_state(),
                interactive.sessions.session().id()
            )
            .last(),
        );
    }
    interactive.shutdown().await;
}

// Covers: generated nested todo updates must be rejected before committing
// uncompactable mandatory state, and the provider must remain reachable.
// Owner: host checklist capacity at the shared direct/nested execution seam.
#[tokio::test]
async fn oversized_nested_todo_keeps_the_previous_list_and_provider_reachable() {
    let accepted = checklist("keep this task");
    let (mut interactive, provider) = todo_runtime(
        vec![
            call("initial", "todo", serde_json::to_value(&accepted).unwrap()),
            text("accepted"),
            call("oversized", "codemode", json!({"script":
                "print(call_tool(\"todo\", {\"todos\": [{\"content\": \"x\" * 1000000, \"status\": \"pending\"}]}))"
            })),
            text("still reachable"),
            text("normal next turn"),
        ],
        None,
    ).await;
    let capacity = rho_sdk::model::context::estimate_context_tokens(
        &interactive.history(),
        &interactive.tools.specs(),
    ) * 2;
    interactive.set_context_window(Some(capacity)).unwrap();
    for _ in 0..3 {
        interactive
            .start(UserInput::text("continue"), None)
            .await
            .unwrap();
        interactive.finish_run().await.unwrap();
        assert_eq!(interactive.todo_list(), Some(accepted.clone()));
    }
    let requests = provider.recorded_requests();
    assert_eq!(requests.len(), 5);
    let nested = requests[3]
        .messages
        .iter()
        .find_map(|message| match message {
            Message::ToolResult(result) if result.id == "oversized" => Some(result),
            _ => None,
        })
        .unwrap();
    assert!(!nested.ok);
    // The nested ToolError is serialized inside the codemode traceback. Check
    // that wire diagnostic, not generic failure (which could hide a script cap).
    assert!(
        nested.content.contains(&format!(
            "todo mandatory context budget exceeded: limit {capacity} estimated tokens, asked "
        )),
        "{}",
        nested.content
    );
    interactive.shutdown().await;
}

// Covers: request admission includes incoming input and refreshes on headless
// turns as well as interactive turns; startup history is not a request budget.
// Owner: host checklist policy at the shared SDK request boundary.
#[tokio::test]
async fn todo_budget_includes_incoming_input_on_interactive_and_headless_turns() {
    use rho_sdk::{
        model::context::{estimate_context_tokens, estimate_messages_tokens},
        RequestContext,
    };

    for interactive_host in [true, false] {
        let generated = checklist(&"x".repeat(4_096));
        let (mut interactive, provider) = todo_runtime(
            vec![
                text("seed history"),
                call("proposal", "codemode", json!({"script":
                    "call_tool(\"todo\", {\"todos\": [{\"content\": \"x\" * 4096, \"status\": \"in_progress\"}]})"
                })),
                text("replacement rejected"),
            ],
            None,
        ).await;
        interactive
            .sessions
            .session()
            .complete("seed")
            .await
            .unwrap();
        let measured = crate::tools::todo::TodoState::default();
        measured.replace(Some(generated));
        let projection = measured.messages(interactive.sessions.session().id());
        // Fits the previous turn exactly, but not the incoming paste. The
        // capacity comes from the actual serialized history and projection.
        let capacity = estimate_context_tokens(&interactive.history(), &interactive.tools.specs())
            + estimate_messages_tokens(&projection);
        interactive.set_context_window(Some(capacity)).unwrap();
        let incoming = "new input ".repeat(200);
        if interactive_host {
            interactive
                .start(UserInput::text(incoming), None)
                .await
                .unwrap();
            interactive.finish_run().await.unwrap();
        } else {
            // ACP and automation use the same SDK session entry directly.
            interactive
                .sessions
                .session()
                .complete(incoming)
                .await
                .unwrap();
        }
        assert_eq!(interactive.todo_list(), None);
        let requests = provider.recorded_requests();
        let result = requests
            .last()
            .unwrap()
            .messages
            .iter()
            .find_map(|message| match message {
                Message::ToolResult(result) if result.id == "proposal" => Some(result),
                _ => None,
            })
            .unwrap();
        assert!(!result.ok);
        assert!(
            result
                .content
                .contains("todo mandatory context budget exceeded"),
            "{}",
            result.content
        );
        interactive.shutdown().await;
    }
}

// Covers: a generated list that fits the prompt/schema budget but crowds out
// real history must not strand compaction or reactivate on the next turn.
// Owner: host runtime compaction and checklist recovery, not TUI rendering.
#[tokio::test]
async fn boundary_nested_todo_compaction_preserves_state_and_provider_recovery() {
    use rho_sdk::{
        model::context::{estimate_context_tokens, estimate_messages_tokens},
        RequestContext,
    };

    let generated = checklist(&"x".repeat(4_096));
    let (mut interactive, provider) = todo_runtime(
        vec![
            call("boundary", "codemode", json!({"script":
                "call_tool(\"todo\", {\"todos\": [{\"content\": \"x\" * 4096, \"status\": \"in_progress\"}]})"
            })),
            text("accepted"),
            text("brief summary"),
            call("clear", "todo", json!({"todos": []})),
            text("recovered"),
        ],
        None,
    ).await;
    let measured = crate::tools::todo::TodoState::default();
    measured.replace(Some(generated.clone()));
    let full = measured.messages(interactive.sessions.session().id());
    // Exactly fits the initial request, including accepted input: no invented
    // reserve. Later tool results and output crowd out the admitted projection.
    let capacity = estimate_context_tokens(&interactive.history(), &interactive.tools.specs())
        + estimate_messages_tokens(&[Message::user_text("track tasks")])
        + estimate_messages_tokens(&full);
    interactive.set_context_window(Some(capacity)).unwrap();
    interactive
        .start(UserInput::text("track tasks"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(interactive.todo_list(), Some(generated.clone()));

    // Even after summarizing the old tool group, the actual replacement needs
    // space beyond the prompt/schema-only admission budget.
    interactive.compact().await.unwrap();
    assert_eq!(provider.recorded_requests().len(), 3);
    assert_eq!(interactive.todo_list(), Some(generated.clone()));
    let snapshot = interactive.sessions.session().snapshot();
    let reader = crate::tools::todo::TodoState::default();
    reader.restore(&snapshot);
    assert_eq!(reader.list(), Some(generated));
    let recovery = interactive
        .tools
        .todo_state()
        .messages(snapshot.session_id());
    assert_ne!(recovery, full);
    assert_eq!(recovery.len(), 1);
    assert!(
        estimate_context_tokens(snapshot.history(), &interactive.tools.specs())
            + estimate_messages_tokens(&recovery)
            <= capacity
    );

    interactive
        .start(UserInput::text("clear tasks"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    let requests = provider.recorded_requests();
    assert_ne!(requests[3].messages.last(), full.last());
    assert!(estimate_context_tokens(&requests[3].messages, &requests[3].tools) <= capacity);
    assert_eq!(
        interactive.todo_list(),
        Some(TodoList { todos: Vec::new() })
    );
    interactive.shutdown().await;
}

// Covers: exact nested state survives real SDK compaction and disk reopen;
// repeated compactions keep request context outside history, clear is durable, and
// selecting a different session or older tree leaf never leaks newer state.
// Owner: interactive host snapshot/compaction and branch lifecycle.
#[tokio::test]
async fn todo_survives_compaction_resume_clear_and_tree_selection() {
    let root = tempfile::tempdir().unwrap();
    let cwd = root.path().join("workspace");
    std::fs::create_dir(&cwd).unwrap();
    let storage = StoredSession::create_in_root(root.path(), &cwd).unwrap();
    let expected = checklist("keep exact order, punctuation, and whitespace\n  second line  ");
    let updated = checklist("unprinted replacement after compaction");
    let (mut interactive, provider) = todo_runtime(
        vec![
            call(
                "nested",
                "codemode",
                json!({"script": format!(
                    "call_tool(\"todo\", {})", serde_json::to_string(&expected).unwrap(),
                )}),
            ),
            text("done"),
            text("summary without a checklist"),
            text("another summary without a checklist"),
            call(
                "update",
                "codemode",
                json!({"script": format!(
                    "call_tool(\"todo\", {})", serde_json::to_string(&updated).unwrap(),
                )}),
            ),
            text("updated"),
            text("inspected resumed replacement"),
            call("clear", "todo", json!({"todos": []})),
            text("cleared"),
            text("inspected resumed clear"),
        ],
        Some(storage.clone()),
    )
    .await;
    interactive
        .start(UserInput::text("track these tasks"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(interactive.todo_list(), Some(expected.clone()));
    let before_compaction = crate::session::tree::SessionTree::load(storage.path())
        .unwrap()
        .active_leaf_id()
        .unwrap()
        .clone();
    for _ in 0..2 {
        interactive
            .sessions
            .session()
            .append_message(Message::assistant_text("y".repeat(8_000)))
            .unwrap();
        interactive
            .sessions
            .session()
            .append_message(Message::assistant_text("recent update"))
            .unwrap();
        assert!(interactive.compact().await.unwrap().is_some());
        assert_eq!(interactive.todo_list(), Some(expected.clone()));
        assert_eq!(
            interactive
                .history()
                .iter()
                .filter(|message| message.as_model_context().is_some())
                .count(),
            0
        );
    }
    let compacted_leaf = crate::session::tree::SessionTree::load(storage.path())
        .unwrap()
        .active_leaf_id()
        .unwrap()
        .clone();
    interactive
        .start(UserInput::text("replace tasks after compaction"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(interactive.todo_list(), Some(updated.clone()));
    let (reopened, _) =
        StoredSession::open_by_id_with_histories_in_root(root.path(), &cwd, storage.id()).unwrap();
    interactive.resume(reopened.clone()).await.unwrap();
    assert_eq!(interactive.todo_list(), Some(updated));
    interactive
        .start(UserInput::text("inspect tasks after resume"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(
        provider.recorded_requests().last().unwrap().messages.last(),
        rho_sdk::RequestContext::messages(
            &interactive.tools.todo_state(),
            interactive.sessions.session().id()
        )
        .last(),
    );

    interactive
        .start(UserInput::text("clear the checklist"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(
        interactive.todo_list(),
        Some(TodoList { todos: Vec::new() })
    );
    interactive.resume(reopened.clone()).await.unwrap();
    assert_eq!(
        interactive.todo_list(),
        Some(TodoList { todos: Vec::new() })
    );
    interactive
        .start(UserInput::text("inspect cleared tasks after resume"), None)
        .await
        .unwrap();
    interactive.finish_run().await.unwrap();
    assert_eq!(
        provider.recorded_requests().last().unwrap().messages.last(),
        rho_sdk::RequestContext::messages(
            &interactive.tools.todo_state(),
            interactive.sessions.session().id()
        )
        .last(),
    );
    interactive
        .select_tree_node(reopened.clone(), &compacted_leaf)
        .await
        .unwrap();
    assert_eq!(interactive.todo_list(), Some(expected.clone()));
    interactive
        .select_tree_node(reopened.clone(), &before_compaction)
        .await
        .unwrap();
    assert_eq!(interactive.todo_list(), Some(expected.clone()));

    let other = StoredSession::create_in_root(root.path(), &cwd).unwrap();
    interactive.resume(other).await.unwrap();
    assert_eq!(interactive.todo_list(), None);
    interactive.resume(reopened).await.unwrap();
    assert_eq!(interactive.todo_list(), Some(expected));
    interactive.reset().await.unwrap();
    assert_eq!(interactive.todo_list(), None);
    interactive.shutdown().await;
}