Files
magpie/internal/sessions/calls_agy.go
T
yetone b30734f964 sessions: Antigravity CLI (agy) conversations are listed, with title, folder, tokens and resume (RickyWei, #1718)
agy's conversations were on no Sessions page: internal/sessions had no
lister for them, only the calls reader of #2097. Now each
~/.gemini/antigravity-cli/brain/<id>/.system_generated/logs/transcript_full.jsonl
is a session (agy.go). Its steps give the start and last times, the first
prompt (taken out of agy's <USER_REQUEST> wrapper), prompts, replies, tool
calls, and tokens under the model "agy", since no step names one. A step
written again replaces its tokens, read on or read whole. The title and
folder come from the conversation's conversation_summaries.db row:
title, and workspace_uris through agyFolder, which turns file:///C:/... and
file:///c%3A/... into C:\... and a host into \\host\share on Windows. agy
writes the title after the transcript, so the row is the file's rev. The
database is read again only when it or its -wal changed, and a failed read
keeps the rows read before. A row with conversations/<id>.db and no
transcript (#1718's case) is listed from the row alone, with no tokens and
no conversation to open. Resume is "agy --conversation <id>". When agy is
set to use magpie, it is the agent's launch command (gateway variables and
--model, the PowerShell form on Windows) with --conversation after it,
through sessions.AgyStart, which package agent sets. The transcript (user
text, thinking, text, tool calls, tool results) can be opened and searched.

Format read from real files: agy 1.3.5 and 1.4.7 in a sandbox HOME
against a fake Gemini endpoint, with plain replies, a read_file tool call
with thinking, invalid tool calls, and a conversation picked up again with
--conversation from another folder. testdata/agy-sessions is three of
those conversations with paths rewritten to /work.

Resume tried for real in the sandbox: `agy --conversation <id> -p again`
from another folder appended steps 2-4 to the same transcript and kept the
row's folder. Interactive `agy --conversation <id>` (in tmux) showed the
earlier turns.

Run at this tree, rebased on origin/main:
- gofmt -l internal clean
- go vet: darwin; GOOS=linux -tags nogui; GOOS=windows
- GOOS=windows go build ./...; GOOS=linux go build -tags nogui ./...
- go test -count=1 ./internal/sessions ./internal/gui ./internal/agent: ok
  before the row-only change. After it, sessions ok, and gui had one red,
  TestMainWindowCorners killed at its 40s deadline at load ~300 (#1936).
  It passes alone (47s).
- GUI (playwright 1.62.0, chromium and webkit, en and zh): i18n-keys,
  gui-de, gui-ja, gui-zh-tw, session-terminal, sessions-bar-names,
  sessions-manage, sessions-talk, sessions-toolbar: 49 of 49. No GUI asset
  changed. Tab names come from the agent registry.

Fails without the change (broken by hand, each one red):
- agyFiles out of allFiles: TestAgySessions, TestAgyTitleLater, TestAgyTranscript
- agy out of refresh's DBRevision check: TestAgyTitleLater
- no dropUse of a rewritten step: TestAgyGrows (4510 input, want 3010)
- AgyStart ignored: TestAgySessions (wired resume)
- drive letter not handled: TestAgyFolder
- agy out of HasTranscript: TestAgySessions, TestAgyTranscript
- the row-only listing off: TestAgyRowOnly; readable() off: TestAgyRowOnly

Not tried: agy on Windows. The Windows box (10.0.0.132) didn't answer
ssh, so the Windows paths are covered by unit tests only (agyFolder,
the Set-Location resume form). A wired resume through magpie's real
gateway was not tried. Not done: subagents (parent_conversation_id),
which I never saw agy write. The model name, which is only in
conversations/<id>.db's protobuf (#3147). Deleting an agy session from magpie. agy in WSL: #3595.

Side effect: agy's self-updater ran in the sandbox and replaced the
live ~/.local/bin/agy, 1.3.5 -> 1.4.7.
2026-10-25 18:24:20 +08:00

115 lines
3.6 KiB
Go

package sessions
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// Antigravity CLI's (agy's) calls (#2097, parkavenue9639: a call agy made
// without magpie was counted nowhere). Each conversation keeps a transcript
// of its steps in
// ~/.gemini/antigravity-cli/brain/<conversation id>/.system_generated/logs/transcript_full.jsonl,
// one JSON object a line. A model's step, as agy 1.2.16 writes it (tried in
// a sandbox against a fake Gemini endpoint):
//
// {"step_index":1,"source":"MODEL","type":"PLANNER_RESPONSE","status":"DONE",
// "created_at":"2026-10-14T06:12:41Z","input_tokens":702,"cache_read_tokens":300,
// "output_tokens":9,"content":"…"}
//
// Those are the step's own counts: input_tokens is the prompt less what was
// read from the cache, and output_tokens holds the thoughts as well as the
// answer, as the gateway counts a Gemini reply. created_at is when the step
// began, to the second. A transcript an older agy wrote has the same lines
// without the token fields; they are left out, not counted as zero.
//
// The step names no model (a custom model's isn't anywhere in agy's files,
// and settings.json's is only the one picked now), nor the account, so the
// call is agy's with the model and provider unknown. A step agy sent through
// magpie is the gateway's to count: usage's matcher pairs it with the
// gateway's record by its tokens and time (agy sends no session header).
// AgyDir is agy's data folder, ~/.gemini/antigravity-cli.
func AgyDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".gemini", "antigravity-cli")
}
// agyCallFiles are each conversation's transcript_full.jsonl.
func agyCallFiles() []file { return agyTranscripts(AgyDir()) }
type agyStep struct {
Step *int `json:"step_index"`
Source string `json:"source"`
CreatedAt string `json:"created_at"`
Input *int `json:"input_tokens"`
CacheRead *int `json:"cache_read_tokens"`
Output *int `json:"output_tokens"`
}
var agyTokens = []byte(`"output_tokens"`)
// readAgyCalls reads a transcript whole: agy may write a step's line again
// as it finishes, and a step is counted once by its index.
func readAgyCalls(f file) (*callFile, bool) {
r, err := os.Open(f.path)
if err != nil {
return &callFile{Path: f.path, Agent: "agy"}, false
}
defer r.Close()
st := &callFile{Agent: "agy", Path: f.path, Session: strings.TrimPrefix(f.key, "agy:")}
at := map[int]int{} // a step's index → its call in st.Calls
br := bufio.NewReaderSize(r, scanBuffer(r, 0))
for {
b, err := br.ReadBytes('\n')
if err != nil && (!errors.Is(err, io.EOF) || len(bytes.TrimSpace(b)) == 0) {
break
}
b = bytes.TrimSpace(b)
if len(b) > 0 && len(b) <= maxLine && bytes.Contains(b, agyTokens) {
var s agyStep
if json.Unmarshal(b, &s) == nil {
if c, ok := agyCall(st, s); ok {
if i, seen := at[*s.Step]; seen {
st.Calls[i] = c
} else {
at[*s.Step] = len(st.Calls)
st.Calls = append(st.Calls, c)
}
}
}
}
if err != nil {
break
}
}
return st, true
}
// agyCall is a model's step as a call.
func agyCall(st *callFile, s agyStep) (Call, bool) {
if s.Source != "MODEL" || s.Step == nil || s.Input == nil || s.Output == nil {
return Call{}, false
}
t, err := time.Parse(time.RFC3339, s.CreatedAt)
if err != nil {
return Call{}, false
}
c := Call{Agent: "agy", Time: t, Session: st.Session, Tokens: Tokens{Input: *s.Input, Output: *s.Output}}
if s.CacheRead != nil {
c.CacheRead = *s.CacheRead
}
if c.Tokens.zero() {
return Call{}, false
}
c.Msg = st.str("agy:" + st.Session + ":" + strconv.Itoa(*s.Step))
return c, true
}