Files
kandev/apps/backend/internal/agent/usage/client_claude_internal_test.go
T
João Salavisa 55c8b461be feat: surface agent subscription usage in settings and chat tooltip (#1697)
* feat: surface agent subscription usage in settings and chat tooltip

Adds live subscription rate-limit visibility for Claude Code and Codex:

- GET /api/v1/agents/usage lists per-agent utilization windows (plan, percent,
  reset time) for host agents with subscription credentials; ?fresh=true
  bypasses the 5-minute cache with a 15s per-provider clamp.
- Fixes both existing usage clients: Claude parsed float utilization as int
  (decode failure) and now reads the richer limits[] array incl. per-model
  weekly windows; Codex read a nonexistent auth.json field and response
  headers - now reads tokens.access_token, parses the body windows, and
  refreshes OAuth tokens on 401. Token refresh persists without dropping
  unknown credential fields.
- Settings > Agents gains a Subscription Usage section; the chat context
  doughnut tooltip shows the session agent's windows, fetched fresh on hover.
- Shared UsageWindowRows component (bars, Good/High/Critical status,
  reset countdowns) keeps settings, tooltip, and office surfaces consistent.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: recompute agent billing type on every read

Subscription billing detection for Claude and Codex was cached with
sync.OnceValue for the process lifetime, so logging in with OAuth after
the backend started never flipped billing badges or office usage checks
until restart. Detection now re-reads the credential files on each call
and reuses the usage clients' HasSubscriptionCredentials checks. Also
adds a regression assertion that fractional-second resets_at timestamps
parse under the RFC3339 layout.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: address subscription usage review feedback

- serialize empty utilization windows as arrays, not null
- sanitize per-agent usage errors in the API (raw provider errors are
  logged server-side; they can carry credential paths/response bodies)
- persist refreshed CLI credentials via atomic temp-file rename
- keep previous settings usage on transient refresh failure; drop stale
  tooltip usage (and its memo) when a fresh fetch fails
- add controller tests for SubscriptionUsage mapping and fresh propagation

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: coalesce concurrent usage fetches and guard codex token refresh

- serialize usage-cache fetches per key with a double-checked lock so
  concurrent fresh requests make one provider call instead of a burst
- reject codex refresh responses with an empty access_token instead of
  persisting them and wiping working credentials
- pin the usage-rows reset fixture 30s under the day boundary so the
  formatted value is deterministic on fast runners

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: coalesce failed usage fetches with short negative caching

Concurrent lookups after a fetch error each retried the provider once
the per-key lock rotated. Failures are now cached for 15 seconds (never
longer than the caller's staleness bound), so bursts share one error
result and the provider gets a breather before the next retry.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: keep stale usage through failed refreshes and skip caching cancellations

Track the last success and last failure independently in the usage
cache: a failed fresh refresh no longer evicts a still-valid success
(non-fresh callers keep the stale value while fresh callers share the
recent error), and a caller-cancelled context no longer records a
negative entry that would penalize other callers.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test: synchronize concurrent usage-cache tests with a channel barrier

Replaces the sleep-widened race window in the coalescing tests with a
started/release barrier per the backend testing guidelines, and dedupes
the two concurrent-caller tests into one helper.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-15 08:50:21 +01:00

196 lines
6.7 KiB
Go

package usage
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
)
func writeClaudeCreds(t *testing.T, dir string, expiresAt int64, extra map[string]any) string {
t.Helper()
oauth := map[string]any{
"accessToken": "live-token",
"refreshToken": "refresh-token",
"expiresAt": expiresAt,
"subscriptionType": "max",
"scopes": []string{"user:inference", "user:profile"},
"rateLimitTier": "default_claude_max_20x",
}
for k, v := range extra {
oauth[k] = v
}
path := filepath.Join(dir, ".credentials.json")
data, err := json.Marshal(map[string]any{"claudeAiOauth": oauth})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, data, 0o600); err != nil {
t.Fatal(err)
}
return path
}
func futureExpiryMillis() int64 {
return time.Now().Add(time.Hour).UnixMilli()
}
// The live API returns float utilization values (e.g. 62.0) — the client must
// not parse them as int.
func TestClaudeFetchUsage_ParsesFloatsAndLimits(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != "Bearer live-token" {
t.Errorf("Authorization = %q", got)
}
if got := r.Header.Get("anthropic-beta"); got != claudeBetaHeader {
t.Errorf("anthropic-beta = %q", got)
}
_, _ = w.Write([]byte(`{
"five_hour": {"utilization": 62.0, "resets_at": "2026-07-14T22:09:59.781046+00:00"},
"seven_day": {"utilization": 15.0, "resets_at": "2026-07-15T00:59:59.781070+00:00"},
"limits": [
{"kind": "session", "group": "session", "percent": 62, "resets_at": "2026-07-14T22:09:59.781046+00:00", "scope": null},
{"kind": "weekly_all", "group": "weekly", "percent": 15, "resets_at": "2026-07-15T00:59:59+00:00", "scope": null},
{"kind": "weekly_scoped", "group": "weekly", "percent": 20.5, "resets_at": "2026-07-15T00:59:59+00:00",
"scope": {"model": {"id": null, "display_name": "Opus"}}},
{"kind": "mystery_future_kind", "percent": 99, "resets_at": "2026-07-15T00:59:59+00:00"}
]
}`))
}))
defer srv.Close()
client := NewClaudeUsageClientWithPath(writeClaudeCreds(t, t.TempDir(), futureExpiryMillis(), nil))
client.usageURL = srv.URL
got, err := client.FetchUsage(context.Background())
if err != nil {
t.Fatalf("FetchUsage: %v", err)
}
if got.Provider != "anthropic" {
t.Errorf("Provider = %q", got.Provider)
}
if got.Plan != "max" {
t.Errorf("Plan = %q, want max", got.Plan)
}
wantLabels := []string{"5-hour", "7-day", "7-day (Opus)"}
if len(got.Windows) != len(wantLabels) {
t.Fatalf("Windows = %+v, want %d entries", got.Windows, len(wantLabels))
}
for i, label := range wantLabels {
if got.Windows[i].Label != label {
t.Errorf("Windows[%d].Label = %q, want %q", i, got.Windows[i].Label, label)
}
}
if got.Windows[0].UtilizationPct != 62 {
t.Errorf("session pct = %v", got.Windows[0].UtilizationPct)
}
if got.Windows[2].UtilizationPct != 20.5 {
t.Errorf("scoped pct = %v", got.Windows[2].UtilizationPct)
}
// The live API emits sub-second fractional timestamps; time.Parse accepts
// them with the RFC3339 layout even though the layout omits fractions.
wantReset := time.Date(2026, 7, 14, 22, 9, 59, 781046000, time.UTC)
if !got.Windows[0].ResetAt.Equal(wantReset) {
t.Errorf("session ResetAt = %v, want %v", got.Windows[0].ResetAt, wantReset)
}
}
func TestClaudeFetchUsage_FallbackWithoutLimits(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{
"five_hour": {"utilization": 33.3, "resets_at": "2026-07-14T22:09:59+00:00"},
"seven_day": {"utilization": 7.0, "resets_at": "2026-07-15T00:59:59+00:00"}
}`))
}))
defer srv.Close()
client := NewClaudeUsageClientWithPath(writeClaudeCreds(t, t.TempDir(), futureExpiryMillis(), nil))
client.usageURL = srv.URL
got, err := client.FetchUsage(context.Background())
if err != nil {
t.Fatalf("FetchUsage: %v", err)
}
if len(got.Windows) != 2 {
t.Fatalf("Windows = %+v, want 2", got.Windows)
}
if got.Windows[0].Label != "5-hour" || got.Windows[0].UtilizationPct != 33.3 {
t.Errorf("window[0] = %+v", got.Windows[0])
}
if got.Windows[1].Label != "7-day" || got.Windows[1].UtilizationPct != 7 {
t.Errorf("window[1] = %+v", got.Windows[1])
}
}
// Refreshing an expired token must not drop unrelated fields from the
// credentials file (scopes, subscriptionType, rateLimitTier, ...).
func TestClaudeFetchUsage_RefreshPreservesUnknownFields(t *testing.T) {
usageSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got != "Bearer new-access" {
t.Errorf("Authorization after refresh = %q", got)
}
_, _ = w.Write([]byte(`{"five_hour": {"utilization": 1.0}, "seven_day": {"utilization": 2.0}}`))
}))
defer usageSrv.Close()
refreshSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"access_token": "new-access", "refresh_token": "new-refresh", "expires_in": 3600}`))
}))
defer refreshSrv.Close()
expired := time.Now().Add(-time.Hour).UnixMilli()
path := writeClaudeCreds(t, t.TempDir(), expired, nil)
client := NewClaudeUsageClientWithPath(path)
client.usageURL = usageSrv.URL
client.refreshURL = refreshSrv.URL
if _, err := client.FetchUsage(context.Background()); err != nil {
t.Fatalf("FetchUsage: %v", err)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var root map[string]map[string]any
if err := json.Unmarshal(data, &root); err != nil {
t.Fatal(err)
}
oauth := root["claudeAiOauth"]
if oauth["accessToken"] != "new-access" || oauth["refreshToken"] != "new-refresh" {
t.Errorf("tokens not persisted: %+v", oauth)
}
if oauth["subscriptionType"] != "max" {
t.Errorf("subscriptionType dropped on refresh: %+v", oauth)
}
if oauth["rateLimitTier"] != "default_claude_max_20x" {
t.Errorf("rateLimitTier dropped on refresh: %+v", oauth)
}
if _, ok := oauth["scopes"]; !ok {
t.Errorf("scopes dropped on refresh: %+v", oauth)
}
}
func TestClaudeHasSubscriptionCredentials(t *testing.T) {
missing := NewClaudeUsageClientWithPath(filepath.Join(t.TempDir(), "nope.json"))
if missing.HasSubscriptionCredentials() {
t.Error("expected false for missing file")
}
apiKeyOnly := filepath.Join(t.TempDir(), ".credentials.json")
if err := os.WriteFile(apiKeyOnly, []byte(`{}`), 0o600); err != nil {
t.Fatal(err)
}
if NewClaudeUsageClientWithPath(apiKeyOnly).HasSubscriptionCredentials() {
t.Error("expected false without claudeAiOauth")
}
withOAuth := NewClaudeUsageClientWithPath(writeClaudeCreds(t, t.TempDir(), futureExpiryMillis(), nil))
if !withOAuth.HasSubscriptionCredentials() {
t.Error("expected true with OAuth credentials")
}
}