rho-coding-agent 1.48.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
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
use std::{path::PathBuf, time::Duration};

use ratatui::text::Line;
use rho_providers::model::{ContextUsage, ContextUsageSource, ModelMetadata, ModelUsage};

use super::{
    command_block::CommandBlock,
    model_performance::ModelPerformanceSummary,
    usage_cost::{
        context_fill_percent, display_input_tokens, format_usd, resolved_context_window,
        resolved_usage_cost_usd_micros, session_total_cost_usd_micros, CostSource,
    },
    workspace::git_branch,
    App, Entry,
};
use crate::claude_runtime::auth::{self, ClaudeProbeSnapshot};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BillingInfo {
    Metered,
    Subscription,
}

impl BillingInfo {
    fn from_provider_auth(provider: &str, auth: &str) -> Self {
        if provider == "openai-codex" || auth == "codex" || auth == "xai-oauth" {
            Self::Subscription
        } else {
            Self::Metered
        }
    }

    fn description(self) -> &'static str {
        match self {
            Self::Metered => "metered API",
            Self::Subscription => "subscription",
        }
    }
}

#[derive(Clone, Debug)]
pub(super) struct RuntimeInfo {
    version: String,
    provider: String,
    model: String,
    reasoning: String,
    permission_mode: String,
    advisor: String,
    billing: BillingInfo,
    cost_source: CostSource,
    cwd: PathBuf,
    branch: Option<String>,
    usage: Option<ModelUsage>,
    latest_usage: Option<ModelUsage>,
    /// Session cache hit rate over durable usage only.
    ///
    /// Kept separate from [`Self::usage`], which folds in the display-only live
    /// stream estimate. That estimate reports fabricated uncached input and no
    /// cache fields, so folding it in would dilute the rate mid-turn.
    session_cache_hit_percent: Option<f64>,
    cache_rebilled: super::cache_stats::CacheRebilled,
    model_performance: ModelPerformanceSummary,
    context_usage: Option<ContextUsage>,
    model_metadata: Option<ModelMetadata>,
    tree: Option<crate::session::tree::SessionTreeFacts>,
    tree_error: Option<String>,
    /// Claude Code auth summary. Outside provider credentials on purpose.
    claude_code: String,
    /// Cumulative cost from all completed subagents, including failed/canceled ones.
    subagent_total_cost_usd_micros: u64,
    /// Cumulative cost from finished advisor calls in this conversation.
    advisor_total_cost_usd_micros: u64,
}

impl App {
    pub(super) async fn execute_info_command(&mut self) -> anyhow::Result<()> {
        let identity = self.info.services.diagnostics.identity();
        let (tree, tree_error) = match self.info.session.session_id.as_deref() {
            Some(_) if self.is_ui_busy() => (
                None,
                Some("tree facts are available after the current model turn".into()),
            ),
            Some(id) => match crate::session::Session::tree_facts_by_id(&self.info.runtime.cwd, id)
            {
                Ok(facts) => (Some(facts), None),
                Err(error) => (None, Some(error.to_string())),
            },
            None => (None, None),
        };
        // During a turn never block stream draining on a Claude child probe.
        let claude_code = if self.is_ui_busy() {
            ClaudeProbeSnapshot::not_refreshed_during_turn().auth_description()
        } else {
            self.claude_probe_snapshot().await.auth_description()
        };
        let info = RuntimeInfo {
            version: identity.rho_version.to_string(),
            provider: identity.provider.to_string(),
            model: identity.model.to_string(),
            reasoning: identity.reasoning.to_string(),
            permission_mode: self.info.runtime.permission_mode.as_str().into(),
            advisor: super::advisor_status::AdvisorStatus::from_runtime(&self.info.runtime)
                .detail(),
            billing: BillingInfo::from_provider_auth(
                &self.info.runtime.provider,
                &self.info.runtime.auth,
            ),
            cost_source: if self.usage.live_stream.is_active() {
                CostSource::Estimated
            } else {
                self.usage.usage_cost_tracker.cumulative_source()
            },
            cwd: self.info.runtime.cwd.clone(),
            branch: git_branch(&self.info.runtime.cwd),
            usage: super::usage_cost::display_usage_with_live(
                self.usage.cumulative_usage.as_ref(),
                &self.usage.live_stream,
                self.model_metadata.as_ref(),
            ),
            latest_usage: self.usage.latest_usage.clone(),
            session_cache_hit_percent: self
                .usage
                .cumulative_usage
                .as_ref()
                .and_then(cache_hit_percent),
            cache_rebilled: self.usage.cache_stats.rebilled().clone(),
            model_performance: self
                .usage
                .model_performance
                .summary(&self.info.runtime.model_call_profile()),
            context_usage: self.usage.current_context.clone(),
            model_metadata: self.model_metadata.clone(),
            tree,
            tree_error,
            claude_code,
            subagent_total_cost_usd_micros: self.usage.subagent_total_cost_usd_micros,
            advisor_total_cost_usd_micros: self.usage.advisor_total_cost_usd_micros,
        };
        self.insert_entry(&Entry::RuntimeInfo(Box::new(info)));
        self.set_status("runtime info");
        Ok(())
    }

    /// Live Claude probe for idle surfaces.
    pub(super) async fn claude_probe_snapshot(&self) -> ClaudeProbeSnapshot {
        auth::probe_snapshot().await
    }
}

pub(super) fn runtime_info_lines(info: &RuntimeInfo, width: usize) -> Vec<Line<'static>> {
    let mut block = CommandBlock::new(width);
    block.push_header("rho", &format!("v{}", info.version));

    block.push_section("Model");
    block.push_field("Provider", &info.provider);
    block.push_field("Model", &info.model);
    block.push_field("Reasoning", &info.reasoning);
    block.push_field("Permissions", &info.permission_mode);
    block.push_field("Advisor", &info.advisor);
    block.push_field("Billing", info.billing.description());

    block.push_section("External runtimes");
    block.push_note(&info.claude_code);

    block.push_section("Session");
    if let Some(tree) = &info.tree {
        block.push_field(
            "Active leaf",
            tree.active_leaf_id
                .as_ref()
                .map_or("none", |id| id.as_str()),
        );
        block.push_field("Nodes", &tree.node_count.to_string());
        block.push_field("Branches", &tree.branch_count.to_string());
    } else if let Some(error) = &info.tree_error {
        block.push_note(&format!("Session tree unavailable: {error}"));
    } else {
        block.push_note("No durable conversation state yet.");
    }

    block.push_section("Session usage");
    push_usage_fields(&mut block, info);

    if let Some(call) = info.model_performance.latest_call {
        let metrics = call.metrics;
        let generation_output_tokens = call.throughput_output_tokens();
        block.push_section("Last model call");
        if let Some(duration) = metrics.time_to_first_token {
            block.push_field("First event", &format_duration(duration));
        }
        if let Some(duration) = metrics.generation_time {
            block.push_field("Generation", &format_duration(duration));
        }
        push_optional_number(&mut block, "Generation tokens", generation_output_tokens);
        push_optional_number(&mut block, "Output tokens", metrics.output_tokens);
        // Compute rates from published metric fields. Do not call
        // ModelCallMetrics::{generation,response}_tokens_per_second here until
        // a released rho-sdk cut exports them; package verify builds against
        // crates.io.
        if let Some(rate) = metrics
            .generation_time
            .and_then(|window| tokens_per_second(generation_output_tokens, window))
        {
            block.push_field("Generation rate", &format!("{rate:.1} tok/s"));
        }
        if let Some(rate) = tokens_per_second(metrics.output_tokens, metrics.total_latency) {
            block.push_field("Response rate", &format!("{rate:.1} tok/s"));
        }
        block.push_field("Total latency", &format_duration(metrics.total_latency));
    }

    if let Some(rate) = info.model_performance.average_generation_tokens_per_second {
        block.push_section("Model performance");
        block.push_field("Average generation", &format!("{rate:.1} tok/s"));
        block.push_field(
            "Samples",
            &info.model_performance.eligible_calls.to_string(),
        );
    }

    block.push_section("Workspace");
    block.push_field("Directory", &info.cwd.display().to_string());
    block.push_field(
        "Git branch",
        info.branch.as_deref().unwrap_or("not in a Git worktree"),
    );
    block.finish()
}

fn push_usage_fields(block: &mut CommandBlock, info: &RuntimeInfo) {
    if let Some(context) =
        format_context_row(info.context_usage.as_ref(), info.model_metadata.as_ref())
    {
        block.push_field("Context", &context);
    } else {
        block.push_field("Context", "not reported");
    }

    let Some(usage) = info.usage.as_ref() else {
        if info.subagent_total_cost_usd_micros == 0 && info.advisor_total_cost_usd_micros == 0 {
            block.push_note("No token usage recorded yet.");
        } else {
            push_cost_fields(block, info, None);
        }
        return;
    };

    push_optional_number(block, "Input tokens", display_input_tokens(usage));
    push_optional_number(block, "Output tokens", usage.output_tokens);
    push_optional_number(block, "Cache read", usage.cache_read_tokens);
    push_optional_number(block, "Cache write", usage.cache_write_tokens);
    if let Some(hit) = format_cache_hit(info) {
        block.push_field("Cache hit", &hit);
    }
    if let Some(rebilled) = format_cache_rebilled(&info.cache_rebilled, info.billing) {
        block.push_field("Cache re-billed", &rebilled);
    }

    let main_cost_micros = resolved_usage_cost_usd_micros(usage, info.model_metadata.as_ref());
    push_cost_fields(block, info, main_cost_micros);
}

fn push_cost_fields(block: &mut CommandBlock, info: &RuntimeInfo, main_cost_micros: Option<u64>) {
    let subagent = info.subagent_total_cost_usd_micros;
    let advisor = info.advisor_total_cost_usd_micros;
    let extra = subagent.saturating_add(advisor);
    let Some(total_micros) = session_total_cost_usd_micros(main_cost_micros, extra) else {
        return;
    };
    let equivalent = cost_equivalent_suffix(info.billing);
    let main_qualifier = if info.cost_source == CostSource::Estimated {
        " estimated"
    } else {
        ""
    };

    for (label, value) in cost_field_rows(
        main_cost_micros,
        subagent,
        advisor,
        total_micros,
        main_qualifier,
        equivalent,
    ) {
        block.push_field(label, &value);
    }
}

/// Labels for /info cost rows. One component stays a single labeled field;
/// multiple components break out Main/Subagent/Advisor plus Total.
fn cost_field_rows(
    main_cost_micros: Option<u64>,
    subagent: u64,
    advisor: u64,
    total_micros: u64,
    main_qualifier: &str,
    equivalent: &str,
) -> Vec<(&'static str, String)> {
    let format_amount =
        |amount: u64, qualifier: &str| format!("{}{qualifier}{equivalent}", format_usd(amount));

    let mut rows = Vec::new();
    if let Some(main) = main_cost_micros {
        rows.push(("Main cost", format_amount(main, main_qualifier)));
    }
    if subagent > 0 {
        rows.push(("Subagent cost", format_amount(subagent, "")));
    }
    if advisor > 0 {
        rows.push(("Advisor cost", format_amount(advisor, "")));
    }

    match rows.len() {
        0 => rows,
        1 => {
            if rows[0].0 == "Main cost" {
                rows[0].0 = "Cost";
            }
            rows
        }
        _ => {
            rows.push(("Total cost", format_amount(total_micros, "")));
            rows
        }
    }
}

fn cost_equivalent_suffix(billing: BillingInfo) -> &'static str {
    if billing == BillingInfo::Subscription {
        " API equivalent"
    } else {
        ""
    }
}

fn push_optional_number(block: &mut CommandBlock, label: &str, value: Option<u64>) {
    if let Some(value) = value {
        block.push_field(label, &format_number(value));
    }
}

fn cache_hit_percent(usage: &ModelUsage) -> Option<f64> {
    let cache_read = usage.cache_read_tokens?;
    let prompt_tokens = usage.inclusive_prompt_tokens()?;
    (prompt_tokens > 0).then(|| cache_read as f64 * 100.0 / prompt_tokens as f64)
}

/// Single cache-hit line covering session and latest-request rates.
fn format_cache_hit(info: &RuntimeInfo) -> Option<String> {
    let latest = info.latest_usage.as_ref().and_then(cache_hit_percent);
    match (info.session_cache_hit_percent, latest) {
        (Some(session), Some(latest)) => Some(format!(
            "{session:.1}% session · {latest:.1}% latest request"
        )),
        (Some(session), None) => Some(format!("{session:.1}% session")),
        (None, Some(latest)) => Some(format!("{latest:.1}% on the latest request")),
        (None, None) => None,
    }
}

/// Session re-bill line for `/info`. Hidden until at least one counted miss.
fn format_cache_rebilled(
    rebilled: &super::cache_stats::CacheRebilled,
    billing: BillingInfo,
) -> Option<String> {
    if rebilled.miss_count == 0 {
        return None;
    }
    let miss_label = if rebilled.miss_count == 1 {
        "1 miss".to_string()
    } else {
        format!("{} misses", rebilled.miss_count)
    };
    let tokens = format_number(rebilled.missed_tokens);
    Some(if rebilled.extra_cost_usd_micros > 0 {
        let cost = format_usd(rebilled.extra_cost_usd_micros);
        let cost = if rebilled.unpriced_miss_count > 0 {
            format!("{cost} partial")
        } else {
            cost
        };
        format!(
            "{cost} ({tokens} tokens, {miss_label}){}",
            cost_equivalent_suffix(billing)
        )
    } else {
        format!("{tokens} tokens ({miss_label})")
    })
}

/// Formats the `/info` context row; shows consumption even when no limit is
/// reported.
fn format_context_row(
    context_usage: Option<&ContextUsage>,
    model_metadata: Option<&ModelMetadata>,
) -> Option<String> {
    let window = resolved_context_window(context_usage, model_metadata);
    let source = match context_usage.map(|usage| usage.source) {
        Some(ContextUsageSource::Estimated) => "estimated",
        Some(ContextUsageSource::ProviderReported) => "provider reported",
        Some(ContextUsageSource::UnknownAfterCompaction) => "unknown after compaction",
        None => "model limit",
    };
    let Some(tokens) = context_usage.and_then(|usage| usage.tokens) else {
        // Nothing reported at all: let the caller render its fallback row.
        let window = window?;
        return Some(format!(
            "unknown / {} tokens ({source})",
            format_number(window)
        ));
    };
    let Some(window) = window else {
        // Without a known limit there is no fill percent; still show consumption.
        return Some(format!("{} tokens ({source})", format_number(tokens)));
    };
    let percent = context_fill_percent(tokens, window);
    Some(format!(
        "{} / {} tokens ({percent:.1}%, {source})",
        format_number(tokens),
        format_number(window)
    ))
}

fn tokens_per_second(tokens: Option<u64>, window: Duration) -> Option<f64> {
    let tokens = tokens?;
    let seconds = window.as_secs_f64();
    (seconds > 0.0).then(|| tokens as f64 / seconds)
}

fn format_duration(duration: Duration) -> String {
    if duration < Duration::from_secs(1) {
        format!("{} ms", duration.as_millis())
    } else {
        format!("{:.1} s", duration.as_secs_f64())
    }
}

fn format_number(value: u64) -> String {
    let digits = value.to_string();
    let mut formatted = String::with_capacity(digits.len() + digits.len() / 3);
    for (index, ch) in digits.chars().enumerate() {
        if index > 0 && (digits.len() - index).is_multiple_of(3) {
            formatted.push(',');
        }
        formatted.push(ch);
    }
    formatted
}

#[cfg(test)]
mod tests {
    use super::*;

    fn labels<'a>(rows: &'a [(&'a str, String)]) -> Vec<&'a str> {
        rows.iter().map(|(label, _)| *label).collect()
    }

    #[test]
    fn cost_rows_cover_single_and_multi_component_layouts() {
        assert_eq!(
            labels(&cost_field_rows(Some(1_000), 0, 0, 1_000, "", "")),
            ["Cost"]
        );
        assert_eq!(
            labels(&cost_field_rows(None, 2_000, 0, 2_000, "", "")),
            ["Subagent cost"]
        );
        assert_eq!(
            labels(&cost_field_rows(None, 0, 3_000, 3_000, "", "")),
            ["Advisor cost"]
        );
        assert_eq!(
            labels(&cost_field_rows(Some(1_000), 0, 3_000, 4_000, "", "")),
            ["Main cost", "Advisor cost", "Total cost"]
        );
        assert_eq!(
            labels(&cost_field_rows(Some(1_000), 2_000, 3_000, 6_000, "", "")),
            ["Main cost", "Subagent cost", "Advisor cost", "Total cost"]
        );
        assert_eq!(
            labels(&cost_field_rows(None, 2_000, 3_000, 5_000, "", "")),
            ["Subagent cost", "Advisor cost", "Total cost"]
        );
    }

    #[test]
    fn cache_hit_percent_uses_total_prompt_tokens() {
        let usage = ModelUsage {
            input_tokens: Some(5_000),
            cache_read_tokens: Some(95_000),
            cache_write_tokens: Some(0),
            ..ModelUsage::default()
        };
        assert_eq!(
            cache_hit_percent(&usage).map(|percent| format!("{percent:.1}")),
            Some("95.0".into())
        );
        // Providers that do not report cache accounting have no rate at all.
        assert_eq!(cache_hit_percent(&ModelUsage::default()), None);
    }

    #[test]
    fn cache_rebilled_field_covers_cost_and_count_forms() {
        use super::super::cache_stats::CacheRebilled;

        assert_eq!(
            format_cache_rebilled(&CacheRebilled::default(), BillingInfo::Metered),
            None
        );
        assert_eq!(
            format_cache_rebilled(
                &CacheRebilled {
                    missed_tokens: 45_230,
                    miss_count: 3,
                    extra_cost_usd_micros: 324_000,
                    unpriced_miss_count: 0,
                },
                BillingInfo::Metered,
            ),
            Some("$0.324 (45,230 tokens, 3 misses)".into())
        );
        assert_eq!(
            format_cache_rebilled(
                &CacheRebilled {
                    missed_tokens: 1_000,
                    miss_count: 1,
                    extra_cost_usd_micros: 0,
                    unpriced_miss_count: 1,
                },
                BillingInfo::Metered,
            ),
            Some("1,000 tokens (1 miss)".into())
        );
        assert_eq!(
            format_cache_rebilled(
                &CacheRebilled {
                    missed_tokens: 2_000,
                    miss_count: 2,
                    extra_cost_usd_micros: 100_000,
                    unpriced_miss_count: 0,
                },
                BillingInfo::Subscription,
            ),
            Some("$0.100 (2,000 tokens, 2 misses) API equivalent".into())
        );
        assert_eq!(
            format_cache_rebilled(
                &CacheRebilled {
                    missed_tokens: 8_000,
                    miss_count: 2,
                    extra_cost_usd_micros: 1_800,
                    unpriced_miss_count: 1,
                },
                BillingInfo::Metered,
            ),
            Some("$0.002 partial (8,000 tokens, 2 misses)".into())
        );
    }

    #[test]
    fn context_details_show_tokens_without_a_reported_limit() {
        // Covers: unknown context window must not hide consumption
        // Owner: /info context row
        assert_eq!(
            format_context_row(Some(&ContextUsage::estimated(123_456, None)), None,),
            Some("123,456 tokens (estimated)".into())
        );
        // A reported limit keeps the familiar tokens/limit/percent form.
        assert_eq!(
            format_context_row(Some(&ContextUsage::estimated(50_000, Some(200_000))), None,),
            Some("50,000 / 200,000 tokens (25.0%, estimated)".into())
        );
    }

    #[test]
    fn context_row_falls_back_when_nothing_is_known() {
        // Covers: no usage and no window must fall back to the caller's
        // "not reported" row instead of naming a limit nobody reported.
        // Owner: /info context row
        assert_eq!(format_context_row(None, None), None);
        // Metadata alone still supplies the window when usage is absent.
        assert_eq!(
            format_context_row(
                None,
                Some(&ModelMetadata {
                    advertised_context_window: Some(200_000),
                    ..ModelMetadata::default()
                }),
            ),
            Some("unknown / 200,000 tokens (model limit)".into())
        );
    }

    #[test]
    fn cost_rows_preserve_main_qualifier_and_amounts() {
        let rows = cost_field_rows(Some(1_500_000), 500_000, 0, 2_000_000, " estimated", "");
        assert_eq!(
            rows,
            vec![
                ("Main cost", "$1.500 estimated".into()),
                ("Subagent cost", "$0.500".into()),
                ("Total cost", "$2.000".into()),
            ]
        );
    }
}