pi_agent_rust 0.3.0

Native AI coding agent CLI - Rust port of Pi Agent
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
//! Provider usage/quota readers and the `/usage` surface (bd-cv653.7.4).
//!
//! Read-only GETs against the quota/credit endpoints providers actually
//! expose (OpenRouter credits, Moonshot balance, GitHub Copilot entitlement),
//! normalized into [`ProviderUsage`] rows so users see quota walls before a
//! 429 does. Providers without a public endpoint (Anthropic, OpenAI expose
//! rate-limit state only in response headers) report `Unavailable` with the
//! reason instead of failing. A failed or slow read never blocks anything:
//! every fetch is timeout-bounded and falls back to the last cached row with
//! an age label.

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use serde::Serialize;

use crate::auth::AuthStorage;
use crate::error::{Error, Result};
use crate::http::client::Client;

/// Schema tag for usage rows in JSON output and RPC events.
pub const USAGE_SCHEMA: &str = "pi.usage.v1";

/// How long a fetched row stays fresh before `/usage` re-reads the endpoint.
pub const USAGE_CACHE_TTL: Duration = Duration::from_secs(60);

/// Per-provider fetch budget; a slow endpoint degrades to cache/unavailable.
pub const USAGE_FETCH_TIMEOUT: Duration = Duration::from_secs(8);

/// Normalized usage/quota snapshot for one provider.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProviderUsage {
    pub provider: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub used: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remaining: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resets_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    pub source: String,
    pub fetched_at_ms: i64,
    /// Set when this row was served from cache instead of a live read.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_age_secs: Option<u64>,
}

/// One `/usage` row: a reading, a documented absence, or a failed read.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase", tag = "status")]
pub enum UsageStatus {
    Ready(ProviderUsage),
    Unavailable { provider: String, reason: String },
    Error { provider: String, error: String },
}

impl UsageStatus {
    #[must_use]
    pub fn provider(&self) -> &str {
        match self {
            Self::Ready(usage) => &usage.provider,
            Self::Unavailable { provider, .. } | Self::Error { provider, .. } => provider,
        }
    }
}

fn now_ms() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX))
}

/// A single provider's quota endpoint reader.
#[async_trait::async_trait]
pub trait UsageReader: Send + Sync {
    fn provider(&self) -> &'static str;
    async fn fetch(&self, client: &Client) -> Result<ProviderUsage>;
}

// ── OpenRouter ──────────────────────────────────────────────────────

/// Reads `GET /api/v1/credits`: purchased vs consumed credits (USD).
pub struct OpenRouterUsageReader {
    api_key: String,
    base_url: String,
}

impl OpenRouterUsageReader {
    #[must_use]
    pub fn new(api_key: String) -> Self {
        Self::with_base_url(api_key, "https://openrouter.ai".to_string())
    }

    #[must_use]
    pub const fn with_base_url(api_key: String, base_url: String) -> Self {
        Self { api_key, base_url }
    }
}

#[async_trait::async_trait]
impl UsageReader for OpenRouterUsageReader {
    fn provider(&self) -> &'static str {
        "openrouter"
    }

    async fn fetch(&self, client: &Client) -> Result<ProviderUsage> {
        let url = format!("{}/api/v1/credits", self.base_url.trim_end_matches('/'));
        let body = get_json(
            client,
            &url,
            &[("Authorization", &format!("Bearer {}", self.api_key))],
        )
        .await?;
        let data = &body["data"]; // ubs:ignore serde_json Value index returns Null, never panics
        let total = data["total_credits"].as_f64(); // ubs:ignore serde_json Value index returns Null, never panics
        let used = data["total_usage"].as_f64(); // ubs:ignore serde_json Value index returns Null, never panics
        Ok(ProviderUsage {
            provider: "openrouter".to_string(),
            plan: None,
            used,
            limit: total,
            remaining: match (total, used) {
                (Some(total), Some(used)) => Some(total - used),
                _ => None,
            },
            unit: Some("USD credits".to_string()),
            resets_at: None,
            detail: None,
            source: url,
            fetched_at_ms: now_ms(),
            cache_age_secs: None,
        })
    }
}

// ── Moonshot / Kimi ─────────────────────────────────────────────────

/// Reads `GET /v1/users/me/balance`: available/voucher/cash balance.
pub struct MoonshotUsageReader {
    api_key: String,
    base_url: String,
}

impl MoonshotUsageReader {
    #[must_use]
    pub fn new(api_key: String) -> Self {
        Self::with_base_url(api_key, "https://api.moonshot.ai".to_string())
    }

    #[must_use]
    pub const fn with_base_url(api_key: String, base_url: String) -> Self {
        Self { api_key, base_url }
    }
}

#[async_trait::async_trait]
impl UsageReader for MoonshotUsageReader {
    fn provider(&self) -> &'static str {
        "moonshotai"
    }

    async fn fetch(&self, client: &Client) -> Result<ProviderUsage> {
        let url = format!(
            "{}/v1/users/me/balance",
            self.base_url.trim_end_matches('/')
        );
        let body = get_json(
            client,
            &url,
            &[("Authorization", &format!("Bearer {}", self.api_key))],
        )
        .await?;
        let data = &body["data"]; // ubs:ignore serde_json Value index returns Null, never panics
        let available = data["available_balance"].as_f64(); // ubs:ignore serde_json Value index returns Null, never panics
        let voucher = data["voucher_balance"].as_f64(); // ubs:ignore serde_json Value index returns Null, never panics
        let cash = data["cash_balance"].as_f64();
        Ok(ProviderUsage {
            provider: "moonshotai".to_string(),
            plan: None,
            used: None,
            limit: None,
            remaining: available,
            unit: Some("balance".to_string()),
            resets_at: None,
            detail: match (voucher, cash) {
                (Some(voucher), Some(cash)) => {
                    Some(format!("voucher {voucher:.2}, cash {cash:.2}"))
                }
                _ => None,
            },
            source: url,
            fetched_at_ms: now_ms(),
            cache_age_secs: None,
        })
    }
}

// ── GitHub Copilot ──────────────────────────────────────────────────

/// Reads the Copilot token endpoint: plan SKU plus limited-user quota
/// counters when present (free-tier accounts).
pub struct CopilotUsageReader {
    github_token: String,
    base_url: String,
}

impl CopilotUsageReader {
    #[must_use]
    pub fn new(github_token: String) -> Self {
        Self::with_base_url(github_token, "https://api.github.com".to_string())
    }

    #[must_use]
    pub const fn with_base_url(github_token: String, base_url: String) -> Self {
        Self {
            github_token,
            base_url,
        }
    }
}

#[async_trait::async_trait]
impl UsageReader for CopilotUsageReader {
    fn provider(&self) -> &'static str {
        "github-copilot"
    }

    async fn fetch(&self, client: &Client) -> Result<ProviderUsage> {
        let url = format!(
            "{}/copilot_internal/v2/token",
            self.base_url.trim_end_matches('/')
        );
        let body = get_json(
            client,
            &url,
            &[
                ("Authorization", &format!("token {}", self.github_token)), // ubs:ignore outbound auth header, local credential
                ("User-Agent", "pi-agent-rust"),
            ],
        )
        .await?;
        let plan = body["sku"].as_str().map(str::to_string); // ubs:ignore serde_json Value index returns Null, never panics
        let chat_quota = body["limited_user_quotas"]["chat"].as_f64(); // ubs:ignore serde_json Value index returns Null, never panics
        let reset = body["limited_user_reset_date"].as_str().map(str::to_string);
        Ok(ProviderUsage {
            provider: "github-copilot".to_string(),
            plan,
            used: None,
            limit: None,
            remaining: chat_quota,
            unit: chat_quota.is_some().then(|| "chat requests".to_string()),
            resets_at: reset,
            detail: body["limited_user_quotas"]["completions"] // ubs:ignore serde_json Value index returns Null, never panics
                .as_f64()
                .map(|c| format!("completions quota {c}")),
            source: url,
            fetched_at_ms: now_ms(),
            cache_age_secs: None,
        })
    }
}

async fn get_json(
    client: &Client,
    url: &str,
    headers: &[(&str, &str)],
) -> Result<serde_json::Value> {
    let mut request = client.get(url);
    for (name, value) in headers {
        request = request.header(*name, *value); // ubs:ignore outbound request header from local auth storage, not request-controlled
    }
    let response = Box::pin(request.send()).await?;
    let status = response.status();
    let text = response
        .text()
        .await
        .unwrap_or_else(|e| format!("<failed to read body: {e}>"));
    if !(200..300).contains(&status) {
        return Err(Error::api(format!("HTTP {status} from {url}: {text}")));
    }
    serde_json::from_str(&text).map_err(|e| Error::api(format!("Invalid JSON from {url}: {e}")))
}

// ── Assembly, cache, rendering ──────────────────────────────────────

/// Providers that hold credentials but expose no public quota endpoint.
const NO_ENDPOINT_REASON: &[(&str, &str)] = &[
    (
        "anthropic",
        "no public quota endpoint (rate-limit state arrives only in response headers)",
    ),
    (
        "openai",
        "no public quota endpoint (usage dashboard requires a browser session)",
    ),
];

/// Readers for configured providers plus (provider, reason) rows for the
/// known no-endpoint set.
pub type ConfiguredReaders = (Vec<Box<dyn UsageReader>>, Vec<(String, String)>);

/// Build readers for every provider with resolvable credentials, plus
/// documented-unavailable rows for known no-endpoint providers.
#[must_use]
pub fn readers_from_auth(auth: &AuthStorage) -> ConfiguredReaders {
    let mut readers: Vec<Box<dyn UsageReader>> = Vec::new();
    let mut unavailable: Vec<(String, String)> = Vec::new();

    if let Some(key) = auth.resolve_api_key("openrouter", None) {
        readers.push(Box::new(OpenRouterUsageReader::new(key)));
    }
    if let Some(key) = auth.resolve_api_key("moonshotai", None) {
        readers.push(Box::new(MoonshotUsageReader::new(key)));
    }
    if let Some(token) = auth.resolve_api_key("github-copilot", None) {
        readers.push(Box::new(CopilotUsageReader::new(token)));
    }
    for (provider, reason) in NO_ENDPOINT_REASON {
        if auth.resolve_api_key(provider, None).is_some() {
            unavailable.push(((*provider).to_string(), (*reason).to_string()));
        }
    }

    (readers, unavailable)
}

static USAGE_CACHE: Mutex<Option<HashMap<String, (Instant, ProviderUsage)>>> = Mutex::new(None);

#[allow(clippy::significant_drop_tightening)] // the guard is the whole body
fn cache_get(provider: &str, max_age: Duration) -> Option<(Duration, ProviderUsage)> {
    let guard = USAGE_CACHE.lock().expect("usage cache lock"); // ubs:ignore poisoned lock means a prior fetch panicked; propagating cannot help
    let cache = guard.as_ref()?;
    let (at, usage) = cache.get(provider)?;
    let age = at.elapsed();
    (age <= max_age).then(|| (age, usage.clone()))
}

fn cache_put(provider: &str, usage: &ProviderUsage) {
    let mut guard = USAGE_CACHE.lock().expect("usage cache lock"); // ubs:ignore poisoned lock means a prior fetch panicked; propagating cannot help
    guard
        .get_or_insert_with(HashMap::new)
        .insert(provider.to_string(), (Instant::now(), usage.clone()));
}

/// Fetch usage for every configured provider.
///
/// Fresh cache hits (younger than [`USAGE_CACHE_TTL`]) short-circuit unless
/// `refresh` forces a live read; on a failed live read, any cached row of ANY
/// age is returned with its age labeled before the error row is considered.
pub async fn gather_usage(auth: &AuthStorage, refresh: bool) -> Vec<UsageStatus> {
    let (readers, unavailable) = readers_from_auth(auth);
    let client = Client::new();
    let mut rows = Vec::new();

    for reader in readers {
        let provider = reader.provider().to_string();
        if !refresh && let Some((age, mut usage)) = cache_get(&provider, USAGE_CACHE_TTL) {
            usage.cache_age_secs = Some(age.as_secs());
            rows.push(UsageStatus::Ready(usage));
            continue;
        }
        let fetched = asupersync::time::timeout(
            asupersync::time::wall_now(),
            USAGE_FETCH_TIMEOUT,
            reader.fetch(&client),
        )
        .await;
        match fetched {
            Ok(Ok(usage)) => {
                cache_put(&provider, &usage);
                rows.push(UsageStatus::Ready(usage));
            }
            Ok(Err(err)) => rows.push(stale_or_error(&provider, &err.to_string())),
            Err(_) => rows.push(stale_or_error(
                &provider,
                &format!("timed out after {}s", USAGE_FETCH_TIMEOUT.as_secs()),
            )),
        }
    }

    for (provider, reason) in unavailable {
        rows.push(UsageStatus::Unavailable { provider, reason });
    }

    rows
}

fn stale_or_error(provider: &str, error: &str) -> UsageStatus {
    cache_get(provider, Duration::MAX).map_or_else(
        || UsageStatus::Error {
            provider: provider.to_string(),
            error: error.to_string(),
        },
        |(age, mut usage)| {
            usage.cache_age_secs = Some(age.as_secs());
            usage.detail = Some(usage.detail.take().map_or_else(
                || format!("live read failed: {error}"),
                |detail| format!("{detail}; live read failed: {error}"),
            ));
            UsageStatus::Ready(usage)
        },
    )
}

/// Render rows as the human-readable `/usage` table.
#[must_use]
pub fn render_usage_text(rows: &[UsageStatus]) -> String {
    if rows.is_empty() {
        return "No providers with credentials configured. Run /login <provider> first."
            .to_string();
    }
    let mut lines = vec!["Provider usage:".to_string()];
    for row in rows {
        match row {
            UsageStatus::Ready(usage) => {
                let mut parts: Vec<String> = Vec::new();
                if let Some(plan) = &usage.plan {
                    parts.push(format!("plan {plan}"));
                }
                match (usage.used, usage.limit) {
                    (Some(used), Some(limit)) => {
                        parts.push(format!("{used:.2} of {limit:.2} used"));
                    }
                    (Some(used), None) => parts.push(format!("{used:.2} used")),
                    _ => {}
                }
                if let Some(remaining) = usage.remaining {
                    parts.push(format!("{remaining:.2} remaining"));
                }
                if let Some(unit) = &usage.unit {
                    parts.push(format!("({unit})"));
                }
                if let Some(resets) = &usage.resets_at {
                    parts.push(format!("resets {resets}"));
                }
                if let Some(detail) = &usage.detail {
                    parts.push(format!("— {detail}"));
                }
                if let Some(age) = usage.cache_age_secs {
                    parts.push(format!("[cached {age}s ago]"));
                }
                if parts.is_empty() {
                    parts.push("no quota data in response".to_string());
                }
                lines.push(format!("  {}: {}", usage.provider, parts.join(" ")));
            }
            UsageStatus::Unavailable { provider, reason } => {
                lines.push(format!("  {provider}: unavailable — {reason}"));
            }
            UsageStatus::Error { provider, error } => {
                lines.push(format!("  {provider}: read failed — {error}"));
            }
        }
    }
    lines.join("\n")
}

/// Render rows as JSON (`pi usage --format json`).
#[must_use]
pub fn render_usage_json(rows: &[UsageStatus]) -> String {
    serde_json::to_string_pretty(&serde_json::json!({
        "schema": USAGE_SCHEMA,
        "providers": rows,
    }))
    .unwrap_or_else(|_| "{}".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{Read as _, Write as _};
    use std::net::TcpListener;

    fn spawn_json_server(status: u16, body: &str) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server"); // ubs:ignore test fixture
        let addr = listener.local_addr().expect("local addr");
        let body = body.to_string();
        std::thread::spawn(move || {
            if let Ok((mut socket, _)) = listener.accept() {
                let mut buffer = [0_u8; 4096];
                let _ = socket.read(&mut buffer);
                let response = format!(
                    "HTTP/1.1 {status} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                );
                let _ = socket.write_all(response.as_bytes());
            }
        });
        format!("http://{addr}")
    }

    fn run_async<F: std::future::Future>(future: F) -> F::Output {
        asupersync::runtime::RuntimeBuilder::current_thread() // ubs:ignore test harness runtime
            .build() // ubs:ignore test harness runtime
            .expect("runtime build")
            .block_on(future)
    }

    #[test]
    fn openrouter_reader_parses_credits() {
        let base = spawn_json_server(200, r#"{"data":{"total_credits":25.0,"total_usage":10.5}}"#);
        let reader = OpenRouterUsageReader::with_base_url("sk-or-test".to_string(), base);
        let usage = run_async(async { reader.fetch(&Client::new()).await.expect("fetch") }); // ubs:ignore test fixture
        assert_eq!(usage.provider, "openrouter");
        assert_eq!(usage.limit, Some(25.0));
        assert_eq!(usage.used, Some(10.5));
        assert_eq!(usage.remaining, Some(14.5));
    }

    #[test]
    fn moonshot_reader_parses_balance() {
        let base = spawn_json_server(
            200,
            r#"{"code":0,"data":{"available_balance":42.5,"voucher_balance":2.5,"cash_balance":40.0},"status":true}"#,
        );
        let reader = MoonshotUsageReader::with_base_url("sk-test".to_string(), base);
        let usage = run_async(async { reader.fetch(&Client::new()).await.expect("fetch") }); // ubs:ignore test fixture
        assert_eq!(usage.provider, "moonshotai");
        assert_eq!(usage.remaining, Some(42.5));
        assert!(usage.detail.as_deref().unwrap().contains("voucher 2.50"));
    }

    #[test]
    fn copilot_reader_surfaces_plan_and_quota() {
        let base = spawn_json_server(
            200,
            r#"{"token":"t","sku":"free_limited_copilot","limited_user_quotas":{"chat":32.0,"completions":100.0},"limited_user_reset_date":"2026-09-01"}"#,
        );
        let reader = CopilotUsageReader::with_base_url("gho_test".to_string(), base);
        let usage = run_async(async { reader.fetch(&Client::new()).await.expect("fetch") }); // ubs:ignore test fixture
        assert_eq!(usage.plan.as_deref(), Some("free_limited_copilot"));
        assert_eq!(usage.remaining, Some(32.0));
        assert_eq!(usage.resets_at.as_deref(), Some("2026-09-01"));
    }

    #[test]
    fn reader_error_on_http_failure() {
        let base = spawn_json_server(401, r#"{"error":"bad key"}"#);
        let reader = OpenRouterUsageReader::with_base_url("sk-bad".to_string(), base);
        let result = run_async(async { reader.fetch(&Client::new()).await });
        let err = result.expect_err("401 must fail");
        assert!(err.to_string().contains("401"), "{err}");
    }

    #[test]
    fn render_text_covers_all_row_kinds() {
        let rows = vec![
            UsageStatus::Ready(ProviderUsage {
                provider: "openrouter".to_string(),
                plan: None,
                used: Some(10.5),
                limit: Some(25.0),
                remaining: Some(14.5),
                unit: Some("USD credits".to_string()),
                resets_at: None,
                detail: None,
                source: "test".to_string(),
                fetched_at_ms: 0,
                cache_age_secs: Some(30),
            }),
            UsageStatus::Unavailable {
                provider: "anthropic".to_string(),
                reason: "no public quota endpoint".to_string(),
            },
            UsageStatus::Error {
                provider: "moonshotai".to_string(),
                error: "timed out".to_string(),
            },
        ];
        let text = render_usage_text(&rows);
        assert!(text.contains("10.50 of 25.00 used"), "{text}");
        assert!(text.contains("[cached 30s ago]"), "{text}");
        assert!(text.contains("anthropic: unavailable"), "{text}");
        assert!(text.contains("moonshotai: read failed"), "{text}");

        let empty = render_usage_text(&[]);
        assert!(empty.contains("/login"), "{empty}");
    }

    #[test]
    fn render_json_is_schema_tagged() {
        let json = render_usage_json(&[UsageStatus::Unavailable {
            provider: "anthropic".to_string(),
            reason: "n/a".to_string(),
        }]);
        let value: serde_json::Value = serde_json::from_str(&json).expect("valid json"); // ubs:ignore test assertion on fixture JSON
        assert_eq!(value["schema"], USAGE_SCHEMA);
        assert_eq!(value["providers"][0]["status"], "unavailable");
    }
}