agentstat 0.1.2

Read-only AI provider usage, quota, balance, and reset data
Documentation
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
//! Integration tests for agentstat local.

#![cfg(feature = "local-files")]
#[cfg(any(feature = "codex", feature = "claude", feature = "cursor"))]
use agentstat::local::CredentialLocation;
#[cfg(any(
    feature = "codex",
    feature = "claude",
    feature = "cursor",
    all(
        feature = "local-probes",
        unix,
        any(
            feature = "coderabbit",
            feature = "antigravity",
            feature = "opencode-local"
        )
    )
))]
use agentstat::*;
#[cfg(any(
    feature = "codex",
    feature = "claude",
    feature = "cursor",
    all(feature = "local-probes", unix)
))]
use std::fs;
#[cfg(all(feature = "local-probes", unix))]
use std::path::{Path, PathBuf};
#[cfg(all(feature = "local-probes", feature = "coderabbit", unix))]
use std::time::Duration;
#[cfg(all(feature = "local-probes", feature = "coderabbit", unix))]
use tokio::time;

#[cfg(all(feature = "opencode-local", unix))]
mod opencode_cli_probe {
    use super::*;
    use std::os::unix::fs::PermissionsExt;

    fn executable(dir: &Path, output: &str) -> PathBuf {
        let path = dir.join("opencode");
        let script = format!(
            "#!/bin/sh\n[ \"$#\" = 4 ] && [ \"$1\" = stats ] && [ \"$2\" = --days ] && [ \"$3\" = 30 ] && [ \"$4\" = --json ] || exit 2\n[ \"$NO_COLOR\" = 1 ] || exit 3\n[ \"$OPENCODE_DISABLE_AUTOUPDATE\" = true ] && [ \"$OPENCODE_DISABLE_PRUNE\" = true ] && [ \"$OPENCODE_DISABLE_MODELS_FETCH\" = true ] || exit 4\ncat <<'OUTPUT'\n{output}\nOUTPUT\n"
        );
        fs::write(&path, script).expect("test fixture file should be writable");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o700))
            .expect("test fixture file should be writable");
        path
    }

    fn text_executable(dir: &Path, report: &str) -> PathBuf {
        let path = dir.join("opencode");
        let script = format!(
            "#!/bin/sh\n[ \"$1\" = stats ] && [ \"$2\" = --days ] && [ \"$3\" = 30 ] || exit 2\nif [ \"$4\" = --json ]; then exit 1; fi\n[ \"$#\" = 3 ] || exit 3\n[ \"$LC_ALL\" = C ] && [ \"$LANG\" = C ] && [ \"$LANGUAGE\" = C ] || exit 4\ncat <<'REPORT'\n{report}\nREPORT\n"
        );
        fs::write(&path, script).expect("test fixture file should be writable");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o700))
            .expect("test fixture file should be writable");
        path
    }

    #[tokio::test]
    async fn local_stats_are_explicit_and_report_activity_without_quota() {
        let dir = tempfile::tempdir().unwrap();
        let path = executable(
            dir.path(),
            r#"{"range":{"from":1767205800000,"to":1792734285567},"sessions":2,"prompts":12,"steps":20,"tokens":{"input":100,"output":50,"reasoning":10,"cache":{"read":70,"write":0}},"cost":1.25,"models":[{"model":{"id":"glm-5","providerID":"opencode-go","variant":"medium"},"tokens":{"input":60,"output":20},"cost":0.75}]}"#,
        );
        let request =
            FetchRequest::new(ProviderId::OpenCode, Credentials::None).source(Source::LocalProbe);
        assert!(matches!(
            Client::builder()
                .build()
                .unwrap()
                .fetch(request.clone())
                .await,
            Err(AgentStatError::LocalAccessDisabled)
        ));
        let snapshot = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::OpenCode, path)
            .build()
            .unwrap()
            .fetch(request)
            .await
            .unwrap();
        assert_eq!(snapshot.source, Source::LocalProbe);
        assert_eq!(snapshot.credential_source, CredentialSource::LocalProbe);
        assert_eq!(snapshot.windows.len(), 10);
        assert_eq!(snapshot.windows[3].used.as_ref().unwrap().as_str(), "100");
        assert!(
            snapshot
                .windows
                .iter()
                .all(|w| matches!(w.limit, QuotaLimit::Unknown))
        );
        assert_eq!(
            snapshot.windows[8].model.as_deref(),
            Some("opencode-go/glm-5#medium")
        );
        assert_eq!(snapshot.spend[0].amount.as_str(), "1.25");
        assert_eq!(snapshot.spend[1].amount.as_str(), "0.75");
    }

    #[tokio::test]
    async fn local_stats_reject_invalid_data_and_scopes() {
        let dir = tempfile::tempdir().unwrap();
        for body in [
            "not JSON",
            r#"{"range":{"from":2,"to":1},"sessions":0}"#,
            r#"{"range":{"from":1767205800000,"to":1792734285567},"sessions":-1,"prompts":0,"steps":0,"tokens":{"input":0,"output":0,"reasoning":0,"cache":{"read":0,"write":0}},"cost":0}"#,
        ] {
            let path = executable(dir.path(), body);
            let error = Client::builder()
                .allow_local_probes(true)
                .probe_executable(ProviderId::OpenCode, path)
                .build()
                .unwrap()
                .fetch(
                    FetchRequest::new(ProviderId::OpenCode, Credentials::None)
                        .source(Source::LocalProbe),
                )
                .await
                .unwrap_err();
            assert!(matches!(error, AgentStatError::InvalidResponse(_)));
        }
        let error = Client::builder()
            .allow_local_probes(true)
            .build()
            .unwrap()
            .fetch(
                FetchRequest::new(ProviderId::OpenCode, Credentials::None)
                    .source(Source::LocalProbe)
                    .scope(AccountScope {
                        workspace_id: Some("wrk_example".into()),
                        ..Default::default()
                    }),
            )
            .await
            .unwrap_err();
        assert!(matches!(error, AgentStatError::Configuration(_)));
    }

    #[tokio::test]
    async fn older_cli_text_report_is_used_when_json_is_unavailable() {
        let dir = tempfile::tempdir().unwrap();
        let report = r#"
┌────────────────────────────────────────┐
│Sessions                          1,234 │
│Messages                         36,626 │
│Total Cost                       $19.10 │
│Input                            194.7M │
│Output                            11.1M │
│Cache Read                      3610.3M │
│Cache Write                           0 │
└────────────────────────────────────────┘
"#;
        let path = text_executable(dir.path(), report);
        let snapshot = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::OpenCode, path)
            .build()
            .unwrap()
            .fetch(
                FetchRequest::new(ProviderId::OpenCode, Credentials::None)
                    .source(Source::LocalProbe),
            )
            .await
            .unwrap();
        assert_eq!(snapshot.source, Source::LocalProbe);
        assert_eq!(snapshot.warnings, vec![Warning::IncompleteData]);
        assert_eq!(snapshot.windows[0].used.as_ref().unwrap().as_str(), "1234");
        assert_eq!(snapshot.windows[1].used.as_ref().unwrap().as_str(), "36626");
        assert_eq!(
            snapshot.windows[2].used.as_ref().unwrap().as_str(),
            "194700000"
        );
        assert_eq!(
            snapshot.windows[4].used.as_ref().unwrap().as_str(),
            "3610300000"
        );
        assert_eq!(snapshot.spend[0].amount.as_str(), "19.1");
        assert!(snapshot.spend[0].starts_at.is_none());
    }

    #[tokio::test]
    async fn legacy_report_rejects_ambiguous_and_malformed_counts() {
        let dir = tempfile::tempdir().expect("temporary directory should be available");
        for (sessions, input, cost) in [
            ("1.234", "100", "$0.00"),
            ("1,2", "100", "$0.00"),
            ("1", "1.2", "$0.00"),
            ("1", "100", "$1,2"),
        ] {
            let report = format!(
                "│Sessions {sessions} │\n│Messages 1 │\n│Total Cost {cost} │\n│Input {input} │\n│Output 20 │\n│Cache Read 0 │\n│Cache Write 0 │"
            );
            let path = text_executable(dir.path(), &report);
            let error = Client::builder()
                .allow_local_probes(true)
                .probe_executable(ProviderId::OpenCode, path)
                .build()
                .expect("client configuration should be valid")
                .fetch(
                    FetchRequest::new(ProviderId::OpenCode, Credentials::None)
                        .source(Source::LocalProbe),
                )
                .await
                .expect_err("malformed legacy statistics must not be reported as usage");
            assert!(
                matches!(error, AgentStatError::InvalidResponse(_)),
                "ambiguous session, token, and cost values must be rejected"
            );
        }
    }

    #[tokio::test]
    async fn free_model_reports_tokens_with_zero_recorded_cost() {
        let dir = tempfile::tempdir().expect("temporary directory should be available");
        let path = executable(
            dir.path(),
            r#"{"range":{"from":1767205800000,"to":1792734285567},"sessions":1,"prompts":1,"steps":1,"tokens":{"input":100,"output":20,"reasoning":0,"cache":{"read":0,"write":0}},"cost":0,"models":[{"model":{"id":"example-free","providerID":"opencode"},"tokens":{"input":100,"output":20},"cost":0}]}"#,
        );
        let snapshot = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::OpenCode, path)
            .build()
            .expect("client configuration should be valid")
            .fetch(
                FetchRequest::new(ProviderId::OpenCode, Credentials::None)
                    .source(Source::LocalProbe),
            )
            .await
            .expect("free-model activity should be reported");
        assert_eq!(
            snapshot.windows[3].used.as_ref().map(Decimal::as_str),
            Some("100")
        );
        assert_eq!(
            snapshot.windows[8].model.as_deref(),
            Some("opencode/example-free")
        );
        assert_eq!(snapshot.spend[0].amount.as_str(), "0");
        assert_eq!(snapshot.spend[1].amount.as_str(), "0");
        assert!(
            snapshot.balances.is_empty(),
            "local statistics do not report credits"
        );
    }
}

#[cfg(feature = "cursor")]
#[tokio::test]
async fn cursor_cli_credentials_are_opt_in_redacted_and_never_modified() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("auth.json");
    let bytes = br#"{"accessToken":"private","refreshToken":"refresh-private"}"#;
    fs::write(&path, bytes).expect("test fixture file should be writable");
    let location = CredentialLocation::Directory(directory.path().into());
    assert!(matches!(
        Client::builder()
            .build()
            .unwrap()
            .load_local_credentials(ProviderId::Cursor, location.clone())
            .await,
        Err(AgentStatError::LocalAccessDisabled)
    ));
    let client = Client::builder().allow_local_files(true).build().unwrap();
    let request = client
        .load_local_credentials(ProviderId::Cursor, location.clone())
        .await
        .unwrap();
    assert!(matches!(request.credentials, Credentials::Bearer(_)));
    assert!(!format!("{request:?}").contains("private"));
    assert_eq!(fs::read(&path).unwrap(), bytes);
    fs::write(&path, br#"{"apiKey":"private"}"#).expect("test fixture file should be writable");
    assert!(matches!(
        client
            .load_local_credentials(ProviderId::Cursor, location)
            .await,
        Err(AgentStatError::InvalidCredentials(ProviderId::Cursor))
    ));
}

#[cfg(all(feature = "local-probes", feature = "antigravity", unix))]
mod antigravity_probe {
    use super::*;
    use std::os::unix::fs::PermissionsExt;

    fn executable(dir: &Path, output: &str) -> PathBuf {
        let path = dir.join("agy");
        let script = format!(
            "#!/bin/sh\n[ \"$#\" = 6 ] || exit 2\n[ \"$1\" = --print ] && [ \"$2\" = /usage ] && [ \"$3\" = --output-format ] && [ \"$4\" = json ] && [ \"$5\" = --log-file ] && [ \"$6\" = /dev/null ] || exit 3\n[ \"$AGY_CLI_DISABLE_AUTO_UPDATE\" = true ] || exit 4\n[ \"$NO_COLOR\" = 1 ] || exit 5\ncat <<'OUTPUT'\n{output}\nOUTPUT\n"
        );
        fs::write(&path, script).expect("test fixture file should be writable");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o700))
            .expect("test fixture file should be writable");
        path
    }

    #[tokio::test]
    async fn structured_usage_probe_requires_opt_in_and_uses_fixed_read_only_arguments() {
        let dir = tempfile::tempdir().unwrap();
        let path = executable(
            dir.path(),
            r#"{"command":{"data":{"groups":[{"name":"Gemini Models","buckets":[{"id":"gemini-weekly","window":"weekly","remaining_fraction":0.6,"reset_time":"2026-10-29T00:00:00Z"},{"id":"gemini-5h","window":"5h","remaining_fraction":0.9}]}]}}}"#,
        );
        let request = FetchRequest::new(ProviderId::Antigravity, Credentials::None)
            .source(Source::LocalProbe);
        assert!(matches!(
            Client::builder()
                .build()
                .unwrap()
                .fetch(request.clone())
                .await,
            Err(AgentStatError::LocalAccessDisabled)
        ));
        let snapshot = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::Antigravity, path)
            .build()
            .unwrap()
            .fetch(request)
            .await
            .unwrap();
        assert_eq!(snapshot.source, Source::LocalProbe);
        assert_eq!(snapshot.credential_source, CredentialSource::LocalProbe);
        assert_eq!(snapshot.windows.len(), 2);
        assert_eq!(snapshot.windows[0].duration_seconds, Some(604800));
        assert_eq!(snapshot.windows[0].utilization_percent, Some(40.0));
        assert_eq!(snapshot.windows[0].model.as_deref(), Some("Gemini Models"));
        assert_eq!(
            snapshot.windows[0].resets_at.unwrap().to_rfc3339(),
            "2026-10-29T00:00:00+00:00"
        );
    }

    #[tokio::test]
    async fn agent_generated_text_and_unknown_probe_schemas_are_rejected() {
        for body in [
            r#"{"result":"You have 50% remaining"}"#,
            r#"{"command":{"data":{"groups":[]}}}"#,
            r#"{"command":{"data":{"groups":[{"buckets":[{"remaining_fraction":-1}]}]}}}"#,
        ] {
            let dir = tempfile::tempdir().unwrap();
            let path = executable(dir.path(), body);
            let err = Client::builder()
                .allow_local_probes(true)
                .probe_executable(ProviderId::Antigravity, path)
                .build()
                .unwrap()
                .fetch(
                    FetchRequest::new(ProviderId::Antigravity, Credentials::None)
                        .source(Source::LocalProbe),
                )
                .await
                .unwrap_err();
            assert!(matches!(err, AgentStatError::InvalidResponse(_)));
        }
    }
}
#[cfg(feature = "codex")]
#[tokio::test]
async fn local_credential_reads_require_opt_in_and_preserve_files() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("auth.json");
    let bytes = br#"{"tokens":{"access_token":"private","account_id":"account-a"}}"#;
    fs::write(&path, bytes).expect("test fixture file should be writable");
    let location = CredentialLocation::Directory(directory.path().into());
    assert!(matches!(
        Client::builder()
            .build()
            .unwrap()
            .load_local_credentials(ProviderId::Codex, location.clone())
            .await,
        Err(AgentStatError::LocalAccessDisabled)
    ));
    let request = Client::builder()
        .allow_local_files(true)
        .build()
        .unwrap()
        .load_local_credentials(ProviderId::Codex, location)
        .await
        .unwrap();
    assert_eq!(request.scope.account_id.as_deref(), Some("account-a"));
    assert_eq!(fs::read(path).unwrap(), bytes);
    assert!(!format!("{request:?}").contains("private"));
}
#[cfg(feature = "claude")]
#[tokio::test]
async fn expired_claude_credentials_are_not_refreshed() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join(".credentials.json");
    let bytes=br#"{"claudeAiOauth":{"accessToken":"private","refreshToken":"refresh-private","expiresAt":1}}"#;
    fs::write(&path, bytes).expect("test fixture file should be writable");
    let client = Client::builder().allow_local_files(true).build().unwrap();
    assert!(matches!(
        client
            .load_local_credentials(
                ProviderId::Claude,
                CredentialLocation::Directory(directory.path().into())
            )
            .await,
        Err(AgentStatError::Authentication)
    ));
    assert_eq!(fs::read(path).unwrap(), bytes);
}
#[cfg(feature = "codex")]
#[tokio::test]
async fn malformed_oversized_and_api_only_credentials_fail_without_leaking() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("auth.json");
    let client = Client::builder().allow_local_files(true).build().unwrap();
    for bytes in [
        br#"{"OPENAI_API_KEY":"private"}"#.to_vec(),
        vec![b'x'; 65537],
        b"private".to_vec(),
    ] {
        fs::write(&path, bytes).expect("test fixture file should be writable");
        let err = client
            .load_local_credentials(
                ProviderId::Codex,
                CredentialLocation::Directory(directory.path().into()),
            )
            .await
            .unwrap_err();
        assert!(!format!("{err} {err:?}").contains("private"));
    }
}
#[cfg(all(feature = "local-probes", feature = "coderabbit", unix))]
mod probes {
    use super::*;
    use std::os::unix::fs::PermissionsExt;
    #[cfg(not(target_os = "linux"))]
    use std::process::Command;
    fn executable(dir: &Path, script: &str) -> PathBuf {
        let path = dir.join("coderabbit");
        fs::write(&path, script).expect("test fixture file should be writable");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o700))
            .expect("test fixture file should be writable");
        path
    }
    #[tokio::test]
    async fn probe_is_opt_in_and_does_not_infer_quota() {
        let dir = tempfile::tempdir().unwrap();
        let path = executable(
            dir.path(),
            "#!/bin/sh\n[ \"$1\" = usage ] || exit 2\nprintf 'Plan: Pro\\nYour reviews: 42\\n'\n",
        );
        let request =
            FetchRequest::new(ProviderId::CodeRabbit, Credentials::None).source(Source::LocalProbe);
        assert!(matches!(
            Client::builder()
                .build()
                .unwrap()
                .fetch(request.clone())
                .await,
            Err(AgentStatError::LocalAccessDisabled)
        ));
        let client = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::CodeRabbit, path)
            .build()
            .unwrap();
        let snapshot = client.fetch(request).await.unwrap();
        assert_eq!(snapshot.windows[0].used.as_ref().unwrap().to_string(), "42");
        assert_eq!(snapshot.windows[0].limit, QuotaLimit::Unknown);
        assert_eq!(snapshot.windows[0].utilization_percent, None);
        assert_eq!(snapshot.plan.as_deref(), Some("Pro"));
    }
    #[tokio::test]
    async fn probe_output_is_bounded() {
        let dir = tempfile::tempdir().unwrap();
        let path = executable(dir.path(), "#!/bin/sh\nhead -c 150000 /dev/zero\n");
        let client = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::CodeRabbit, path)
            .build()
            .unwrap();
        assert!(matches!(
            client
                .fetch(
                    FetchRequest::new(ProviderId::CodeRabbit, Credentials::None)
                        .source(Source::LocalProbe)
                )
                .await,
            Err(AgentStatError::ResponseTooLarge)
        ));
    }
    #[tokio::test]
    async fn timeout_terminates_probe_process_group() {
        let dir = tempfile::tempdir().unwrap();
        let pidfile = dir.path().join("child-pid");
        let script = format!(
            "#!/bin/sh\nsleep 30 &\necho $! > '{}'\nwait\n",
            pidfile.display()
        );
        let path = executable(dir.path(), &script);
        let client = Client::builder()
            .allow_local_probes(true)
            .probe_executable(ProviderId::CodeRabbit, path)
            .timeout(Duration::from_millis(100))
            .build()
            .unwrap();
        assert!(matches!(
            client
                .fetch(FetchRequest::new(ProviderId::CodeRabbit, Credentials::None))
                .await,
            Err(AgentStatError::Timeout)
        ));
        if let Ok(pid) = fs::read_to_string(&pidfile) {
            let pid: i32 = pid.trim().parse().unwrap();
            // Linux may retain a killed grandchild as a zombie until its reaper runs.
            #[cfg(target_os = "linux")]
            for _ in 0..20 {
                match fs::read_to_string(format!("/proc/{pid}/stat")) {
                    Err(_) => return,
                    Ok(stat) if stat.split_whitespace().nth(2) == Some("Z") => return,
                    _ => time::sleep(Duration::from_millis(10)).await,
                }
            }
            // SIGKILL is delivered asynchronously. On macOS, a terminated child
            // can also remain visible to kill(pid, 0) while it is a zombie.
            #[cfg(not(target_os = "linux"))]
            for _ in 0..100 {
                let output = Command::new("ps")
                    .args(["-o", "stat=", "-p", &pid.to_string()])
                    .output()
                    .unwrap();
                let state = String::from_utf8_lossy(&output.stdout);
                if !output.status.success()
                    || state.trim().is_empty()
                    || state.trim().starts_with('Z')
                {
                    return;
                }
                time::sleep(Duration::from_millis(20)).await;
            }
            panic!("probe descendant is still running");
        }
    }
}