Repository navigation
feat(daemon): read Antigravity quota with agy usage --json
#369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,3 +97,92 @@ func TestAntigravityUsageSyncService_SkipsUnchangedDBs(t *testing.T) { | |
| assert.Equal(t, int32(2), parseCalls.Load()) | ||
| assert.Equal(t, int32(2), backfillHits.Load()) | ||
| } | ||
|
|
||
| func TestAntigravityUsageSyncService_SendsQuotaFromAgyUsage(t *testing.T) { | ||
| origList := listProcessNamesFunc | ||
| origRoots := antigravityDataRootsFunc | ||
| origCollect := collectAntigravityDBFilesFunc | ||
| origQuota := fetchAntigravityQuotaFunc | ||
| t.Cleanup(func() { | ||
| listProcessNamesFunc = origList | ||
| antigravityDataRootsFunc = origRoots | ||
| collectAntigravityDBFilesFunc = origCollect | ||
| fetchAntigravityQuotaFunc = origQuota | ||
| }) | ||
|
|
||
| type window struct { | ||
| LimitID string `json:"limit_id"` | ||
| LimitName string `json:"limit_name"` | ||
| GroupName string `json:"group_name"` | ||
| GroupDescription string `json:"group_description"` | ||
| UsagePercentage float64 `json:"usage_percentage"` | ||
| ResetsAt string `json:"resets_at"` | ||
| WindowDurationMinutes int `json:"window_duration_minutes"` | ||
| } | ||
| var got struct { | ||
| SchemaVersion int `json:"schema_version"` | ||
| Windows []window `json:"windows"` | ||
| } | ||
| var quotaHits atomic.Int32 | ||
| var auth string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (optional): |
||
| if r.URL.Path != "/api/v1/antigravity-usage" { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| return | ||
| } | ||
| quotaHits.Add(1) | ||
| auth = r.Header.Get("Authorization") | ||
| _ = json.NewDecoder(r.Body).Decode(&got) | ||
| w.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| running := true | ||
| listProcessNamesFunc = func() ([]string, error) { | ||
| if running { | ||
| return []string{"/Applications/Antigravity.app/Contents/MacOS/Antigravity"}, nil | ||
| } | ||
| return []string{"zsh"}, nil | ||
| } | ||
| antigravityDataRootsFunc = func() []string { return nil } | ||
| collectAntigravityDBFilesFunc = func([]string, time.Time) ([]string, error) { return nil, nil } | ||
|
|
||
| var quotaCalls atomic.Int32 | ||
| reset := time.Date(2026, 12, 8, 15, 18, 15, 0, time.UTC) | ||
| fetchAntigravityQuotaFunc = func(context.Context) ([]model.AntigravityQuotaWindow, error) { | ||
| quotaCalls.Add(1) | ||
| return []model.AntigravityQuotaWindow{ | ||
| {LimitID: "gemini-weekly", LimitName: "Weekly Limit Remaining", GroupName: "Gemini Models", GroupDescription: "Models within this group: Gemini 4 Neon", RemainingFraction: 0.75, ResetAt: reset, WindowDurationMinutes: 10080}, | ||
| {LimitID: "3p-5h", LimitName: "Five Hour Limit Remaining", GroupName: "Claude and GPT models", RemainingFraction: 1, WindowDurationMinutes: 300}, | ||
| }, nil | ||
| } | ||
|
|
||
| svc := NewAntigravityUsageSyncService(model.ShellTimeConfig{Token: "st_test", APIEndpoint: srv.URL}) | ||
| svc.sync(context.Background()) | ||
|
|
||
| assert.Equal(t, int32(1), quotaHits.Load()) | ||
| assert.Equal(t, "CLI st_test", auth) | ||
| assert.Equal(t, 1, got.SchemaVersion) | ||
| if assert.Len(t, got.Windows, 2) { | ||
| assert.Equal(t, window{ | ||
| LimitID: "gemini-weekly", LimitName: "Weekly Limit Remaining", GroupName: "Gemini Models", | ||
| GroupDescription: "Models within this group: Gemini 4 Neon", UsagePercentage: 25, | ||
| ResetsAt: "2026-12-08T15:18:15Z", WindowDurationMinutes: 10080, | ||
| }, got.Windows[0]) | ||
| assert.Equal(t, "", got.Windows[1].ResetsAt, "a window that has not started has no reset time") | ||
| assert.Zero(t, got.Windows[1].UsagePercentage) | ||
| } | ||
|
|
||
| // Without Antigravity running, agy is not asked. | ||
| running = false | ||
| svc.sync(context.Background()) | ||
| assert.Equal(t, int32(1), quotaCalls.Load()) | ||
|
|
||
| // agy missing is skipped quietly and nothing is sent. | ||
| running = true | ||
| fetchAntigravityQuotaFunc = func(context.Context) ([]model.AntigravityQuotaWindow, error) { | ||
| return nil, model.ErrAgyNotInstalled | ||
| } | ||
| svc.sync(context.Background()) | ||
| assert.Equal(t, int32(1), quotaHits.Load()) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (optional): the quota pass runs inline in
sync, before the token sync, and nothing in it watchesstopChan.agy usage --jsoncan take up to 20 s and the POST 5 s.Stop()(which callswg.Wait()) can therefore block about 25 s if the daemon context is not cancelled first, and a hung agy delays the token backfill by the same time on every tick. A failed quota call also logs Warn on every cycle, and an older server answering 404 on/api/v1/antigravity-usagemakesSendHTTPRequestJSONlog Error plus Warn every 10 minutes (the Cursor sync behaves the same). Fix: run the quota pass after the token sync, or in its own goroutine tied tostopChan, and treatHTTPStatusError404/405 as a quiet skip.