Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a Grok Build session read before is read on from where it was left, not whole

yetoned9f6b42cbcde
diff --git a/internal/sessions/grok.go b/internal/sessions/grok.go
index a0bb3b7ee..a679067d7 100644
--- a/internal/sessions/grok.go
+++ b/internal/sessions/grok.go
@@ -132,78 +132,121 @@ var (
grokHook = []byte(`"sessionUpdate":"hook_execution"`)
)
-// parseGrok reads a session's updates whole, with its summary.
-func parseGrok(f file) *state {
- s := &state{Size: f.size, Mod: f.mod.UnixNano()}
- var since time.Time
- model := ""
+// grokCarry is what a read of a Grok Build session's updates carries on
+// with: where it read to, what of its summary the updates were counted
+// under, and its first prompt as far as it was read.
+type grokCarry struct {
+ Tail *tail `json:"tail,omitempty"`
+ Created time.Time `json:"created,omitzero"`
+ Since time.Time `json:"since,omitzero"`
+ Model string `json:"model,omitempty"`
+ Prompt string `json:"prompt,omitempty"`
+ InPrompt bool `json:"in_prompt,omitempty"`
+ // the first prompt, before it is told from a tag
+ Title string `json:"title,omitempty"`
+}
+
+// parseGrok reads a session's updates, with its summary. Grok Build only
+// appends to updates.jsonl, so a session read before is read on from where
+// that read left off; its summary, rewritten as it goes on, is read again
+// each time. When the file was cut short or replaced, or the summary now
+// says another time or model the updates are counted under, it is read
+// whole again.
+func parseGrok(f file, old *state) *state {
+ var c grokCarry
+ var m grokSummary
+ hasM := false
if f.manifest != "" {
- if m, ok := readGrokSummary(f.manifest); ok {
- s.ID, s.Cwd, model = m.Info.ID, m.Info.Cwd, m.CurrentModel
- s.Named = title(m.GeneratedTitle)
- if s.Named == "" {
- s.Named = title(m.SessionSummary)
- }
- created, _ := time.Parse(time.RFC3339Nano, m.CreatedAt)
+ if m, hasM = readGrokSummary(f.manifest); hasM {
+ c.Model = m.CurrentModel
+ c.Created, _ = time.Parse(time.RFC3339Nano, m.CreatedAt)
if m.Parent != "" {
// what it copied from its source is older than it
- since = created.Truncate(time.Second)
+ c.Since = c.Created.Truncate(time.Second)
}
- s.saw(created, f.main)
+ } else {
+ m = grokSummary{}
}
}
- prompt, inPrompt := "", false
- scan(f.path, 0, func(b []byte) {
- at := numTS(b)
- if !since.IsZero() && at.Before(since) {
- return
+ var s *state
+ var off int64
+ if o := old; o != nil && o.Grok != nil && o.Grok.Model == c.Model && o.Grok.Created.Equal(c.Created) && o.Grok.Since.Equal(c.Since) && o.Grok.Tail.holds(f.path) {
+ s, c, off = o.clone(), *o.Grok, o.Grok.Tail.Off
+ } else {
+ s = &state{}
+ if hasM {
+ s.saw(c.Created, f.main)
}
- if bytes.Contains(b, grokHook) {
- // its hooks run as it starts and ends too: a session left open
- // overnight isn't at work till it is closed
- return
- }
- s.saw(at, f.main)
- user := f.main && s.Title == "" && bytes.Contains(b, grokUser)
- if !user && inPrompt {
- // the first prompt's chunks are over
- s.Title, inPrompt = title(prompt), false
+ }
+ s.Size, s.Mod = f.size, f.mod.UnixNano()
+ s.ID, s.Cwd, s.Named = m.Info.ID, m.Info.Cwd, title(m.GeneratedTitle)
+ if s.Named == "" {
+ s.Named = title(m.SessionSummary)
+ }
+ end, err := scanAt(f.path, off, nil, func(b []byte, _, _ int64) bool {
+ grokUpdate(s, &c, b, f.main)
+ return true
+ })
+ c.Tail = nil
+ if err == nil {
+ c.Tail = markTail(f.path, end, 0, end)
+ }
+ t := c.Title
+ if c.InPrompt {
+ t = title(c.Prompt)
+ }
+ s.Title, s.First = t, ""
+ if strings.HasPrefix(t, "<") {
+ s.Title, s.First = "", untagged(t)
+ }
+ s.Grok = &c
+ return s
+}
+
+// grokUpdate counts one line of a session's updates.
+func grokUpdate(s *state, c *grokCarry, b []byte, main bool) {
+ at := numTS(b)
+ if !c.Since.IsZero() && at.Before(c.Since) {
+ return
+ }
+ if bytes.Contains(b, grokHook) {
+ // its hooks run as it starts and ends too: a session left open
+ // overnight isn't at work till it is closed
+ return
+ }
+ s.saw(at, main)
+ user := main && c.Title == "" && bytes.Contains(b, grokUser)
+ if !user && c.InPrompt {
+ // the first prompt's chunks are over
+ c.Title, c.Prompt, c.InPrompt = title(c.Prompt), "", false
+ }
+ if !user && !bytes.Contains(b, grokTurn) {
+ return
+ }
+ var l grokLine
+ if json.Unmarshal(b, &l) != nil {
+ return
+ }
+ u := l.Params.Update
+ switch u.SessionUpdate {
+ case "user_message_chunk":
+ if user && u.Content.Type == "text" {
+ c.Prompt += u.Content.Text
+ c.InPrompt = true
}
- if !user && !bytes.Contains(b, grokTurn) {
+ case "turn_completed":
+ if u.Usage == nil {
return
}
- var l grokLine
- if json.Unmarshal(b, &l) != nil {
+ date := dateOf(at)
+ if len(u.Usage.ModelUsage) == 0 {
+ s.use(date, cmp.Or(c.Model, "grok"), u.Usage.tokens())
return
}
- u := l.Params.Update
- switch u.SessionUpdate {
- case "user_message_chunk":
- if user && u.Content.Type == "text" {
- prompt += u.Content.Text
- inPrompt = true
- }
- case "turn_completed":
- if u.Usage == nil {
- return
- }
- date := dateOf(at)
- if len(u.Usage.ModelUsage) == 0 {
- s.use(date, cmp.Or(model, "grok"), u.Usage.tokens())
- return
- }
- for name, mu := range u.Usage.ModelUsage {
- s.use(date, name, mu.tokens())
- }
+ for name, mu := range u.Usage.ModelUsage {
+ s.use(date, name, mu.tokens())
}
- })
- if inPrompt {
- s.Title = title(prompt)
- }
- if strings.HasPrefix(s.Title, "<") {
- s.Title, s.First = "", untagged(s.Title)
}
- return s
}
var tsKey = []byte(`"timestamp":`)
diff --git a/internal/sessions/grow_test.go b/internal/sessions/grow_test.go
new file mode 100644
index 000000000..46bb38fa2
--- /dev/null
+++ b/internal/sessions/grow_test.go
@@ -0,0 +1,182 @@
+package sessions
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// grower reads one session file as refresh does, each parse on from the
+// one kept before, and checks every such parse against a whole read of the
+// same file: a summary read on from where it was is the one a whole read
+// makes. start is where the last parse began reading the file.
+type grower struct {
+ t *testing.T
+ files func() []file
+ path string
+ old *state
+ start int64
+}
+
+func (g *grower) file() file {
+ g.t.Helper()
+ for _, f := range g.files() {
+ if f.path == g.path {
+ return f
+ }
+ }
+ g.t.Fatalf("no file %s", g.path)
+ return file{}
+}
+
+// read parses the file on from the last parse and says where it started.
+func (g *grower) read() int64 {
+ g.t.Helper()
+ f := g.file()
+ g.start = -1
+ scanHook = func(p string, off int64) {
+ if p == g.path && g.start < 0 {
+ g.start = off
+ }
+ }
+ defer func() { scanHook = nil }()
+ s := parse(f, g.old)
+ scanHook = nil
+ whole := parse(f, nil)
+ got, _ := json.Marshal(s)
+ want, _ := json.Marshal(whole)
+ if !bytes.Equal(got, want) {
+ g.t.Fatalf("read on from %d:\n%s\nread whole:\n%s", g.start, got, want)
+ }
+ g.old = s
+ return g.start
+}
+
+func (g *grower) size() int64 {
+ g.t.Helper()
+ fi, err := os.Stat(g.path)
+ if err != nil {
+ g.t.Fatal(err)
+ }
+ return fi.Size()
+}
+
+func (g *grower) append(s string) {
+ g.t.Helper()
+ f, err := os.OpenFile(g.path, os.O_APPEND|os.O_WRONLY, 0)
+ if err != nil {
+ g.t.Fatal(err)
+ }
+ defer f.Close()
+ if _, err := f.WriteString(s); err != nil {
+ g.t.Fatal(err)
+ }
+}
+
+// grows runs the cases every reader of an appended file meets: a line
+// appended is the only thing read; half a line waits for the rest and is
+// counted once; a file cut short, or replaced by one as long, is read whole
+// again. line is a whole line the file may take, with its newline.
+func checkGrows(t *testing.T, g *grower, line string) {
+ t.Helper()
+ orig, err := os.ReadFile(g.path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if at := g.read(); at != 0 {
+ t.Fatalf("first read started at %d", at)
+ }
+ // a line appended: only it is read
+ was := g.size()
+ g.append(line)
+ if at := g.read(); at != was {
+ t.Fatalf("after a line appended to %d bytes, read from %d", was, at)
+ }
+ // half a line, still being written: it waits for the rest
+ was = g.size()
+ half := len(line) / 2
+ g.append(line[:half])
+ if at := g.read(); at != was {
+ t.Fatalf("with half a line at %d, read from %d", was, at)
+ }
+ g.append(line[half:])
+ if at := g.read(); at != was {
+ t.Fatalf("the half line finished at %d, read from %d", was, at)
+ }
+ // cut short: read whole again
+ if err := os.WriteFile(g.path, orig, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if at := g.read(); at != 0 {
+ t.Fatalf("cut short, read from %d", at)
+ }
+ // replaced by another as long, its first line changed: read whole
+ g.append(line)
+ g.read()
+ b, _ := os.ReadFile(g.path)
+ other := append([]byte(nil), b...)
+ other[len(orig)-2] ^= 1 // a line it had, changed in place
+ if bytes.Equal(other, b) {
+ t.Fatal("not changed")
+ }
+ if err := os.WriteFile(g.path, other, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if at := g.read(); at != 0 {
+ t.Fatalf("rewritten in place, read from %d", at)
+ }
+}
+
+// Grok Build appends to a session's updates.jsonl: a turn that ended is
+// read alone, not the session again.
+func TestGrokReadsOnlyWhatWasAppended(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ dir := filepath.Join(home, ".grok", "sessions", "%2Fwork%2Fgrok", grokMain)
+ g := &grower{t: t, files: grokFiles, path: filepath.Join(dir, "updates.jsonl")}
+ turn := `{"timestamp":1790496140,"method":"_x.ai/session/update","params":{"sessionId":"` + grokMain + `","update":{"sessionUpdate":"turn_completed","usage":{"inputTokens":200,"outputTokens":6,"cachedReadTokens":100,"cacheCreationTokens":0,"modelUsage":{"grok-4.7-build":{"inputTokens":200,"outputTokens":6,"cachedReadTokens":100,"cacheCreationTokens":0}}}}}}` + "\n"
+ checkGrows(t, g, turn)
+
+ // its summary rewritten, a title given: nothing of the updates read
+ g.read()
+ summary := filepath.Join(dir, "summary.json")
+ b, _ := os.ReadFile(summary)
+ os.WriteFile(summary, bytes.Replace(b, []byte(`"generated_title": "Parser port"`), []byte(`"generated_title": "Port of the parser"`), 1), 0o644)
+ if at, end := g.read(), g.size(); at != end || g.old.Named != "Port of the parser" {
+ t.Fatalf("summary changed: read from %d of %d, named %q", at, end, g.old.Named)
+ }
+ // the model its turns are counted under changed: read whole
+ b, _ = os.ReadFile(summary)
+ os.WriteFile(summary, bytes.Replace(b, []byte(`"current_model_id": "grok-4.7"`), []byte(`"current_model_id": "grok-5"`), 1), 0o644)
+ if at := g.read(); at != 0 {
+ t.Fatalf("model changed: read from %d", at)
+ }
+}
+
+// A first prompt still coming in chunks when a read ends goes on in the
+// next one.
+func TestGrokPromptAcrossReads(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ dir := filepath.Join(home, ".grok", "sessions", "%2Fwork%2Fgrok", "0190aaaa-0000-7000-8000-0000000000ee")
+ os.MkdirAll(dir, 0o755)
+ os.WriteFile(filepath.Join(dir, "summary.json"), []byte(`{"info":{"id":"0190aaaa-0000-7000-8000-0000000000ee","cwd":"/w"},"created_at":"2026-09-27T08:00:00Z"}`), 0o644)
+ chunk := func(ts int, text string) string {
+ return fmt.Sprintf(`{"timestamp":%d,"params":{"update":{"sessionUpdate":"user_message_chunk","content":{"type":"text","text":%q}}}}`+"\n", ts, text)
+ }
+ p := filepath.Join(dir, "updates.jsonl")
+ os.WriteFile(p, []byte(chunk(1790496001, "Port the ")), 0o644)
+ g := &grower{t: t, files: grokFiles, path: p}
+ g.read()
+ g.append(chunk(1790496002, "parser"))
+ g.read()
+ g.append(`{"timestamp":1790496003,"params":{"update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"ok"}}}}` + "\n")
+ g.read()
+ g.append(chunk(1790496004, "and more"))
+ if g.read(); g.old.Title != "Port the parser" {
+ t.Fatalf("title %q", g.old.Title)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index dd96027b5..8e1be82df 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -210,6 +210,9 @@ type state struct {
// Pi: in a forked session, the time it was forked; the lines before
// it are the copy of the session it was forked from
Since time.Time `json:"since,omitzero"`
+ // Grok Build: where its updates were read to, and what the read
+ // carries on with
+ Grok *grokCarry `json:"grok,omitempty"`
}
// day is one local date's share of a file: tokens by model, and the time
@@ -1170,7 +1173,10 @@ func assemble(fs []file, price func(string) *catalog.Price) (Session, bool) {
// parserFor is the source of truth for both parsing and window admission.
// A default line reader is Claude-compatible, including both Qoder variants.
type sessionParser struct {
- whole func(file) *state
+ whole func(file) *state
+ // grow reads a file on from where the kept parse of it left off, when
+ // the file has only grown since (tail.go), else whole
+ grow func(file, *state) *state
line func(*state, []byte, bool)
claude bool
}
@@ -1188,7 +1194,7 @@ func parserFor(agent string) sessionParser {
case "cline":
return sessionParser{whole: parseCline}
case "grok":
- return sessionParser{whole: parseGrok}
+ return sessionParser{grow: parseGrok}
case "kimi":
return sessionParser{whole: parseKimi}
case "droid":
@@ -1217,6 +1223,9 @@ func parse(f file, old *state) *state {
if parser.whole != nil {
return parser.whole(f)
}
+ if parser.grow != nil {
+ return parser.grow(f, old)
+ }
// Every source using claudeLine must rebuild a missing transient window.
if parser.claude && old != nil && old.Claude == nil {
old = nil
diff --git a/internal/sessions/tail.go b/internal/sessions/tail.go
new file mode 100644
index 000000000..8c6cbbc98
--- /dev/null
+++ b/internal/sessions/tail.go
@@ -0,0 +1,50 @@
+package sessions
+
+import "os"
+
+// tail is where a reader of a file another agent appends to left off: the
+// offset after the last whole line it handled, and enough of the file as it
+// was to know it again. A summary kept with one reads on from Off when the
+// file has only grown, instead of reading it whole again (Grok Build's
+// sessions are kept so). A file that shrank, or whose
+// start or sampled prefix is no longer what was read, is read whole again:
+// it was rewritten or replaced, not appended to.
+//
+// A packed file (frames of zstd) is read on from the frame at Off, Skip
+// bytes into what that frame holds, and Size there is the end of that frame:
+// the bytes hashed always take in what is read again.
+type tail struct {
+ Off int64 `json:"off"`
+ Skip int64 `json:"skip,omitempty"`
+ Size int64 `json:"size"`
+ Head string `json:"head"`
+ HeadSize int `json:"head_size"`
+ Hash string `json:"hash"`
+}
+
+// markTail is the tail of a file read up to off, its first size bytes the
+// ones read and to be found again.
+func markTail(path string, off, skip, size int64) *tail {
+ head := headOf(path)
+ if int64(len(head)) > size {
+ head = head[:size]
+ }
+ h := prefixHash(path, size)
+ if h == "" && size > 0 {
+ return nil
+ }
+ return &tail{Off: off, Skip: skip, Size: size, Head: hashHead(head), HeadSize: len(head), Hash: h}
+}
+
+// holds says whether the file at path is still the one t was read from,
+// grown or as it was, so it can be read on from t.Off.
+func (t *tail) holds(path string) bool {
+ if t == nil {
+ return false
+ }
+ fi, err := os.Stat(path)
+ if err != nil || fi.Size() < t.Size || t.Off > t.Size {
+ return false
+ }
+ return sameHead(headOf(path), t.Head, t.HeadSize) && prefixHash(path, t.Size) == t.Hash
+}