agy wrote its calls nowhere magpie read. Each conversation's brain/<id>/.system_generated/logs/transcript_full.jsonl has a line per step, and a model step carries its own input_tokens, cache_read_tokens and output_tokens. readAgyCalls reads those as agent agy, provider unknown (session-unknown) and no model: the step names none, and settings.json's is only the one picked now. A transcript an older agy wrote has the lines with no token fields; they are left out, not counted as zero. agy sends the gateway no session header, so its gateway records name no session and the matcher, which pairs by session, would have counted a step sent through magpie twice. matchedBlocks now pairs an agy step with a session-less agy gateway record by tokens alone, and lets the record end up to 30 minutes after the step's created_at, which is when the step began (to the second), not when its call ended. Tried for real: agy 1.2.16 under a sandbox HOME, with GEMINI_API_KEY and GOOGLE_GEMINI_BASE_URL on a fake Gemini endpoint, three turns of one conversation. It sent User-Agent google-genai-sdk/1.72.0, X-Goog-Api-Key and no session header; its transcript's input_tokens is the prompt less the cached tokens and output_tokens includes thoughtsTokenCount (8 + 40 = 48), as the gateway counts a Gemini reply. A turn that failed to connect wrote ERROR_MESSAGE steps with no tokens. That transcript is the fixture beside the issue's quoted lines and an old one without tokens; the built binary's `magpie usage --csv all` in that HOME lists the three steps as agy, log. Run: TestAgyCalls and TestAgyStepThroughMagpieCountedOnce fail without the change (0 agy calls; "the step the gateway logged is counted again"); gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on sessions and usage; go build darwin, linux nogui, windows; go test -tags nogui of internal/sessions, internal/usage, internal/usageapi, internal/cli in a sandbox HOME, all ok. Not tried: a step through a real magpie gateway and a real Google account (the matcher's pairing is covered by the unit test with the gateway's numbers from the fake run); agy on Windows or Linux; the full go test ./...
55 lines
2.1 KiB
Go
55 lines
2.1 KiB
Go
package sessions
|
|
|
|
import (
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// #2097 (parkavenue9639): Antigravity CLI's own calls reach the usage
|
|
// ledger. testdata/agy/brain holds three conversations: one agy 1.2.16
|
|
// wrote in a sandbox against a fake Gemini endpoint (three model steps,
|
|
// one with 40 thought tokens in its output, and a turn that failed to
|
|
// connect five times), one made of the lines the issue quotes, and one an
|
|
// older agy wrote, whose model step has no token fields.
|
|
func TestAgyCalls(t *testing.T) {
|
|
claude, _ := setup(t)
|
|
copyTree(t, "testdata/agy", filepath.Join(filepath.Dir(claude), "home", ".gemini", "antigravity-cli"))
|
|
|
|
var got []Call
|
|
for _, c := range Calls(time.Time{}) {
|
|
if c.Agent == "agy" {
|
|
got = append(got, c)
|
|
}
|
|
}
|
|
slices.SortFunc(got, func(a, b Call) int { return a.Time.Compare(b.Time) })
|
|
want := []struct {
|
|
at string
|
|
session string
|
|
tokens Tokens
|
|
}{
|
|
{"2026-10-14T03:03:17Z", "2b0e6c1a-1d3e-4f6a-9a51-0f1c2d3e4f5a", Tokens{Input: 14559, Output: 2116}},
|
|
{"2026-10-14T03:04:02Z", "2b0e6c1a-1d3e-4f6a-9a51-0f1c2d3e4f5a", Tokens{Input: 3013, CacheRead: 14973, Output: 1518}},
|
|
{"2026-10-14T06:12:41Z", "8ac63d53-9162-4a29-af29-bda3428931ae", Tokens{Input: 702, CacheRead: 300, Output: 9}},
|
|
{"2026-10-14T06:15:01Z", "8ac63d53-9162-4a29-af29-bda3428931ae", Tokens{Input: 701, CacheRead: 300, Output: 8}},
|
|
{"2026-10-14T06:16:06Z", "8ac63d53-9162-4a29-af29-bda3428931ae", Tokens{Input: 701, CacheRead: 300, Output: 48}},
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("want %d agy calls (none from the transcript without token fields), got %d: %+v", len(want), len(got), got)
|
|
}
|
|
for i, w := range want {
|
|
c := got[i]
|
|
at, _ := time.Parse(time.RFC3339, w.at)
|
|
if !c.Time.Equal(at) || c.Session != w.session || c.Tokens != w.tokens {
|
|
t.Errorf("call %d: %v %s %+v, want %s %s %+v", i, c.Time, c.Session, c.Tokens, w.at, w.session, w.tokens)
|
|
}
|
|
if c.Model != "" {
|
|
t.Errorf("call %d names model %q, which agy's step doesn't", i, c.Model)
|
|
}
|
|
if c.Msg == "" {
|
|
t.Errorf("call %d: no step id to count it once by", i)
|
|
}
|
|
}
|
|
}
|