rho-coding-agent 2.9.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
490
491
492
493
494
495
use pretty_assertions::assert_eq;
use rho_providers::credentials::MemoryCredentialStore;
use std::{collections::BTreeMap, time::Instant};

use super::*;
use crate::usage_limits::{ProviderUsageLimits, UsageLimitWindow, UsageProviderKind};
use crate::usage_limits_cache::UsageLimitsCache;

fn sample_claude_state(observed_at_unix: i64) -> crate::claude_runtime::rate_limit::RateLimitState {
    let mut state = crate::claude_runtime::rate_limit::RateLimitState::default();
    state.merge_window(crate::claude_runtime::rate_limit::RateLimitObservation {
        observed_at_unix,
        observed_seq: 1,
        observed_at_nanos: u64::try_from(observed_at_unix.max(0))
            .unwrap_or(0)
            .saturating_mul(1_000_000_000),
        observed_nonce: "test".into(),
        info: crate::cli_runtime::stream_effect::RateLimitInfo {
            status: Some("allowed".into()),
            rate_limit_type: Some("five_hour".into()),
            resets_at: Some(1_800_000_000),
            utilization: None,
            overage_status: None,
            overage_resets_at: None,
            is_using_overage: Some(false),
        },
    });
    state
}

fn codex_window() -> UsageLimitWindow {
    UsageLimitWindow {
        label: "Weekly".into(),
        remaining_percent: Some(40.0),
        resets_at_unix: Some(now_unix() + 3_600),
        note: None,
    }
}

// Covers: /limits must open a popup instead of inserting a transcript row or
// queuing a model turn.
// Owner: interactive TUI (unit seam; PTY covers the visible overlay)
#[test]
fn opening_limits_does_not_queue_model_context() {
    let mut app = super::super::tests::test_app();
    app.begin_provider_turn_ui();
    app.start_limits_command();

    assert!(app.pending.steering_prompts().is_empty());
    assert!(app.pending.queued_prompts().is_empty());
    assert!(matches!(
        app.input_ui.composer(),
        super::super::ComposerMode::Limits(_)
    ));
    assert!(
        app.history
            .entries()
            .iter()
            .all(|entry| !matches!(entry, super::super::Entry::Error(_))),
        "limits overlay must not dump a transcript error, got {:?}",
        app.history.entries()
    );
}

#[tokio::test]
async fn cancelling_limits_query_waits_for_background_task_to_stop() {
    let mut app = super::super::tests::test_app();
    let task_marker = std::sync::Arc::new(());
    let captured_marker = task_marker.clone();
    app.pending_usage_limits.push(PendingUsageFetch {
        id: LimitsSectionId::Provider(UsageProviderKind::Codex),
        handle: tokio::spawn(async move {
            let _marker = captured_marker;
            std::future::pending::<LimitsFetchResult>().await
        }),
    });

    app.cancel_limits_command().await;

    assert!(app.pending_usage_limits.is_empty());
    assert_eq!(std::sync::Arc::strong_count(&task_marker), 1);
}

#[test]
fn formats_reset_relative_only_within_one_day() {
    assert_eq!(format_reset_at(200_000, 200_000 - 90 * 60), "in 1h 30m");
    assert!(!format_reset_at(200_000, 0).starts_with("in "));
}

// Covers: a throttled usage read names the rate limit in the heading instead
// of the generic failure copy, with and without a cached snapshot.
// Owner: pure unit
#[test]
fn failed_heading_distinguishes_rate_limit() {
    let cases = [
        (
            UsageFailure::RateLimited,
            Some(900),
            "rate limited · showing last known",
        ),
        (
            UsageFailure::RateLimited,
            None,
            "rate limited · try again in a moment",
        ),
        (UsageFailure::Other, Some(900), "update failed"),
        (UsageFailure::Other, None, "unavailable"),
    ];
    let observed: Vec<String> = cases
        .iter()
        .map(|(reason, cached_at_unix, _)| {
            heading_status(
                &LimitsSection {
                    id: LimitsSectionId::ClaudeCode,
                    label: CLAUDE_CODE_PROVIDER_LABEL.into(),
                    status: LimitsSectionStatus::Failed {
                        cached_at_unix: *cached_at_unix,
                        reason: *reason,
                    },
                    windows: Vec::new(),
                },
                None,
                1_000,
            )
        })
        .collect();
    let expected: Vec<String> = cases.iter().map(|(_, _, text)| (*text).into()).collect();
    assert_eq!(observed, expected);
}

// Covers: after a failed probe the cached disk windows stay visible; only a
// rate-limited refresh names the throttle in the heading, other failures keep
// the quiet "last seen" heading, and a missing cache reads as a plain failure.
// Owner: pure unit
#[test]
fn disk_fallback_status_follows_failure_reason() {
    let state = sample_claude_state(900);
    let cases: [(Option<UsageFailure>, LimitsSectionStatus); 3] = [
        (
            Some(UsageFailure::RateLimited),
            LimitsSectionStatus::Failed {
                cached_at_unix: Some(900),
                reason: UsageFailure::RateLimited,
            },
        ),
        (
            Some(UsageFailure::Other),
            LimitsSectionStatus::Observed {
                observed_at_unix: 900,
            },
        ),
        (
            None,
            LimitsSectionStatus::Observed {
                observed_at_unix: 900,
            },
        ),
    ];
    for (failed, expected) in cases {
        let mut overlay = empty_overlay();
        super::limits_claude::apply_claude_disk_state(&mut overlay, Some(&state), failed, 1_000);
        let section = &overlay.sections[0];
        assert_eq!(section.status, expected, "{failed:?}");
        assert_eq!(section.windows.len(), 1, "{failed:?}");
    }

    let mut overlay = empty_overlay();
    super::limits_claude::apply_claude_disk_state(
        &mut overlay,
        None,
        Some(UsageFailure::RateLimited),
        1_000,
    );
    assert_eq!(
        overlay.sections[0].status,
        LimitsSectionStatus::Failed {
            cached_at_unix: None,
            reason: UsageFailure::RateLimited,
        }
    );
    assert!(overlay.sections[0].windows.is_empty());

    let mut overlay = empty_overlay();
    super::limits_claude::apply_claude_disk_state(&mut overlay, None, None, 1_000);
    assert!(overlay.sections.is_empty());
}

fn empty_overlay() -> LimitsOverlay {
    LimitsOverlay {
        sections: Vec::new(),
        empty_note: None,
        scroll: Default::default(),
        checking_started: Instant::now(),
    }
}

// Covers: a live fetch for one provider must not wait on the others.
// Owner: pure unit
#[test]
fn applying_one_provider_leaves_others_checking() {
    let mut overlay = LimitsOverlay {
        sections: vec![
            LimitsSection {
                id: LimitsSectionId::Provider(UsageProviderKind::Codex),
                label: UsageProviderKind::Codex.label().into(),
                status: LimitsSectionStatus::Checking {
                    cached_at_unix: None,
                },
                windows: Vec::new(),
            },
            LimitsSection {
                id: LimitsSectionId::Provider(UsageProviderKind::KimiCode),
                label: UsageProviderKind::KimiCode.label().into(),
                status: LimitsSectionStatus::Checking {
                    cached_at_unix: None,
                },
                windows: Vec::new(),
            },
        ],
        empty_note: None,
        scroll: Default::default(),
        checking_started: Instant::now(),
    };
    overlay.apply_live(
        LimitsSectionId::Provider(UsageProviderKind::Codex),
        UsageProviderKind::Codex.label(),
        vec![codex_window()],
    );

    assert_eq!(overlay.sections[0].status, LimitsSectionStatus::Live);
    assert_eq!(overlay.sections[0].windows.len(), 1);
    assert!(matches!(
        overlay.sections[1].status,
        LimitsSectionStatus::Checking {
            cached_at_unix: None
        }
    ));
    assert!(overlay.sections[1].windows.is_empty());
}

// Covers: reopening /limits after a live fetch must not invent an epoch-zero
// age when the on-disk cache is missing.
// Owner: pure unit
#[test]
fn live_ready_section_uses_in_memory_fetch_time_not_cache() {
    let windows = vec![codex_window()];
    let mut live = BTreeMap::new();
    live.insert(
        UsageProviderKind::Codex,
        LiveUsage::Ready {
            limits: ProviderUsageLimits {
                provider: UsageProviderKind::Codex.label().into(),
                windows: windows.clone(),
            },
            fetched_at_unix: 1_700,
        },
    );
    let section = provider_section(
        UsageProviderKind::Codex,
        &live,
        &[],
        &UsageLimitsCache::default(),
    );
    assert_eq!(section.status, LimitsSectionStatus::Live);
    assert_eq!(section.windows, windows);
}

// Covers: Claude observations appear without a live probe even with no OAuth.
// Owner: pure unit
#[test]
fn claude_section_is_present_without_oauth() {
    let overlay = build_limits_overlay(
        &MemoryCredentialStore::default(),
        &BTreeMap::new(),
        &[],
        UsageLimitsCache::default(),
        super::limits_claude::ClaudeLimitsView {
            disk: Some(&sample_claude_state(1_000)),
            pending: false,
            probe_available: false,
        },
        1_125,
    );
    assert_eq!(overlay.sections.len(), 1);
    assert_eq!(overlay.sections[0].id, LimitsSectionId::ClaudeCode);
    assert_eq!(overlay.sections[0].windows.len(), 1);
    assert_eq!(overlay.sections[0].windows[0].label, "5-hour");
    assert!(overlay.sections[0].windows[0].remaining_percent.is_none());
    assert!(matches!(
        overlay.sections[0].status,
        LimitsSectionStatus::Observed {
            observed_at_unix: 1_000
        }
    ));
}

#[test]
fn claude_section_surfaces_utilization_without_allowed_status() {
    let mut state = crate::claude_runtime::rate_limit::RateLimitState::default();
    let mut five = sample_claude_state(1_000).windows.remove(0);
    five.info.utilization = Some(0.25);
    five.info.status = Some("allowed_warning".into());
    let mut weekly = five.clone();
    weekly.info.rate_limit_type = Some("seven_day".into());
    weekly.info.utilization = Some(0.4);
    weekly.info.status = Some("allowed".into());
    weekly.observed_at_unix = 1_050;
    weekly.observed_at_nanos = 1_050_000_000_000;
    state.merge_window(five);
    state.merge_window(weekly);

    let section = super::limits_claude::claude_provider_limits(
        super::limits_claude::ClaudeLimitsView {
            disk: Some(&state),
            pending: false,
            probe_available: false,
        },
        1_100,
    )
    .expect("claude");
    assert_eq!(section.windows[0].label, "5-hour");
    assert_eq!(section.windows[0].remaining_percent, Some(75.0));
    assert_eq!(section.windows[1].label, "Weekly");
    assert_eq!(section.windows[1].remaining_percent, Some(60.0));
}

// Covers: Live keeps cache-only windows but labels them with observed age.
// Owner: pure unit
#[test]
fn fresh_probe_state_shows_live_including_disk_fable() {
    let mut disk = sample_claude_state(1_100);
    disk.windows[0].info.utilization = Some(0.0);
    let mut weekly = disk.windows[0].clone();
    weekly.info.rate_limit_type = Some("seven_day".into());
    weekly.info.utilization = Some(0.19);
    weekly.observed_at_unix = 1_100;
    let mut fable = weekly.clone();
    fable.info.rate_limit_type = Some("seven_day_fable".into());
    fable.info.utilization = Some(0.33);
    fable.observed_at_unix = 1_000;
    fable.observed_at_nanos = 1_000_000_000_000;
    disk.merge_window(weekly);
    disk.merge_window(fable);
    disk.last_probe_unix = Some(1_100);

    let section = super::limits_claude::claude_provider_limits(
        super::limits_claude::ClaudeLimitsView {
            disk: Some(&disk),
            pending: false,
            probe_available: false,
        },
        1_100,
    )
    .expect("claude");
    assert_eq!(section.status, LimitsSectionStatus::Live);
    let labels: Vec<&str> = section
        .windows
        .iter()
        .map(|window| window.label.as_str())
        .collect();
    assert_eq!(labels, vec!["5-hour", "Weekly", "Fable"]);
    assert_eq!(section.windows[0].note, None);
    assert_eq!(section.windows[1].note, None);
    assert_eq!(section.windows[2].note.as_deref(), Some("observed 1m ago"));
}

// Covers: a live Claude probe must occupy /limits before any disk cache exists.
// Owner: pure unit
#[test]
fn stale_disk_probe_with_binary_shows_checking() {
    let mut disk = sample_claude_state(1_000);
    disk.last_probe_unix = Some(1);
    let overlay = build_limits_overlay(
        &MemoryCredentialStore::default(),
        &BTreeMap::new(),
        &[],
        UsageLimitsCache::default(),
        super::limits_claude::ClaudeLimitsView {
            disk: Some(&disk),
            pending: false,
            probe_available: true,
        },
        1_000 + 400,
    );
    assert!(matches!(
        overlay.sections[0].status,
        LimitsSectionStatus::Checking {
            cached_at_unix: Some(1)
        }
    ));
}

#[test]
fn claude_probe_without_disk_shows_checking() {
    let overlay = build_limits_overlay(
        &MemoryCredentialStore::default(),
        &BTreeMap::new(),
        &[],
        UsageLimitsCache::default(),
        super::limits_claude::ClaudeLimitsView {
            disk: None,
            pending: false,
            probe_available: true,
        },
        0,
    );
    assert_eq!(overlay.sections.len(), 1);
    assert_eq!(overlay.sections[0].id, LimitsSectionId::ClaudeCode);
    assert!(matches!(
        overlay.sections[0].status,
        LimitsSectionStatus::Checking {
            cached_at_unix: None
        }
    ));
}

#[test]
fn missing_claude_state_does_not_invent_a_section() {
    let overlay = build_limits_overlay(
        &MemoryCredentialStore::default(),
        &BTreeMap::new(),
        &[],
        UsageLimitsCache::default(),
        super::limits_claude::ClaudeLimitsView {
            disk: None,
            pending: false,
            probe_available: false,
        },
        0,
    );
    assert!(overlay.sections.is_empty());
    assert!(overlay.empty_note.is_some());
}

// Covers: bars share one label column across providers so they line up.
// Owner: pure unit
#[test]
fn overlay_body_uses_global_window_label_column() {
    let overlay = LimitsOverlay {
        sections: vec![
            LimitsSection {
                id: LimitsSectionId::Provider(UsageProviderKind::Codex),
                label: UsageProviderKind::Codex.label().into(),
                status: LimitsSectionStatus::Live,
                windows: vec![UsageLimitWindow {
                    label: "5-hour".into(),
                    remaining_percent: Some(40.0),
                    resets_at_unix: None,
                    note: None,
                }],
            },
            LimitsSection {
                id: LimitsSectionId::Provider(UsageProviderKind::Xai),
                label: UsageProviderKind::Xai.label().into(),
                status: LimitsSectionStatus::Live,
                windows: vec![UsageLimitWindow {
                    label: "Monthly".into(),
                    remaining_percent: Some(80.0),
                    resets_at_unix: None,
                    note: None,
                }],
            },
        ],
        empty_note: None,
        scroll: Default::default(),
        checking_started: Instant::now(),
    };
    let lines = overlay_body_lines(&overlay, 80, None, 10);
    let texts: Vec<String> = lines
        .iter()
        .map(|line| {
            line.spans
                .iter()
                .map(|span| span.content.as_ref())
                .collect()
        })
        .collect();
    let five = texts
        .iter()
        .find(|text| text.contains("5-hour"))
        .expect("5-hour row");
    let monthly = texts
        .iter()
        .find(|text| text.contains("Monthly"))
        .expect("Monthly row");
    let five_bar = five
        .find('')
        .or_else(|| five.find(''))
        .expect("codex bar");
    let monthly_bar = monthly
        .find('')
        .or_else(|| monthly.find(''))
        .expect("xai bar");
    assert_eq!(five_bar, monthly_bar);
}