Skip to content

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

sessions: a Droid transcript read before is read on from where it was left, not whole

yetoneb9b4c21c5bc1
diff --git a/internal/sessions/droid.go b/internal/sessions/droid.go
index cbf7473ff..885688508 100644
--- a/internal/sessions/droid.go
+++ b/internal/sessions/droid.go
@@ -153,68 +153,46 @@ func droidTools(raw json.RawMessage) []string {
return out
}
-// parseDroid reads a session's transcript whole, with its settings.
-func parseDroid(f file) *state {
- s := &state{Size: f.size, Mod: f.mod.UnixNano()}
- // a fork's lines up to upto are copies of its source's
- upto, copied := "", false
- first := true
- scan(f.path, 0, func(b []byte) {
- var l droidLine
- if json.Unmarshal(b, &l) != nil {
- return
- }
- if first {
- first = false
- var st droidStart
- if l.Type == "session_start" && json.Unmarshal(b, &st) == nil {
- s.ID, s.Cwd, s.Named = st.ID, st.Cwd, title(st.Title)
- if s.Named == "New Session" {
- s.Named = "" // Droid's until it names one
- }
- if st.LastCwd != "" {
- s.Cwd = st.LastCwd
- }
- upto, copied = st.Forked, st.Forked != ""
- }
- return
- }
- if l.Type != "message" {
- return
- }
- if copied {
- if l.ID == upto {
- copied = false
- }
- return
- }
- at := tsAt(b, false)
- s.saw(at, f.main)
- switch l.Message.Role {
- case "user":
- text := droidText(l.Message.Content)
- if strings.TrimSpace(text) == "" || !f.main {
- return
- }
- if strings.HasPrefix(strings.TrimSpace(text), "<") {
- if s.First == "" {
- s.First = untagged(text)
- }
- return
- }
- s.day(dateOf(at)).Prompts++
- if s.Title == "" {
- s.Title = title(text)
- }
- case "assistant":
- if f.main {
- s.day(dateOf(at)).Replies++
- }
- for _, name := range droidTools(l.Message.Content) {
- s.tool(at, name, "")
- }
- }
+// droidCarry is what a read of a Droid transcript carries on with: where
+// it read to, whether it has passed the session_start line and a fork's
+// copied lines, and what its settings' total was counted as, to be taken
+// back when the total moves on.
+type droidCarry struct {
+ Tail *tail `json:"tail,omitempty"`
+ Started bool `json:"started,omitempty"`
+ Upto string `json:"upto,omitempty"`
+ Copied bool `json:"copied,omitempty"`
+ // the settings' total, as counted
+ Date string `json:"date,omitempty"`
+ Model string `json:"model,omitempty"`
+ Tokens Tokens `json:"tokens,omitzero"`
+}
+
+// parseDroid reads a session's transcript, with its settings. Droid only
+// appends to a transcript, so one read before is read on from where that
+// read left off, and its settings' running total, counted on the day of
+// the last message, is taken back and counted again as it is now. A
+// transcript cut short or replaced is read whole again.
+func parseDroid(f file, old *state) *state {
+ var s *state
+ var c droidCarry
+ var off int64
+ if old != nil && old.Droid != nil && old.Droid.Tail.holds(f.path) {
+ s, c, off = old.clone(), *old.Droid, old.Droid.Tail.Off
+ s.dropUse(c.Date, c.Model, c.Tokens)
+ } else {
+ s = &state{}
+ }
+ s.Size, s.Mod = f.size, f.mod.UnixNano()
+ end, err := scanAt(f.path, off, nil, func(b []byte, _, _ int64) bool {
+ droidLineOf(s, &c, b, f.main)
+ return true
})
+ c.Tail = nil
+ if err == nil {
+ c.Tail = markTail(f.path, end, 0, end)
+ }
+ c.Date, c.Model, c.Tokens = "", "", Tokens{}
if f.manifest != "" {
var st droidSettings
if b, err := os.ReadFile(f.manifest); err == nil && json.Unmarshal(b, &st) == nil && st.Usage != nil {
@@ -223,12 +201,74 @@ func parseDroid(f file) *state {
when = time.Unix(0, s.Mod)
}
u := st.Usage
- s.use(dateOf(when), droidModelName(st.Model), Tokens{Input: u.Input, Output: u.Output, CacheRead: u.CacheRead, CacheWrite: u.CacheWrite})
+ c.Date, c.Model = dateOf(when), droidModelName(st.Model)
+ c.Tokens = Tokens{Input: u.Input, Output: u.Output, CacheRead: u.CacheRead, CacheWrite: u.CacheWrite}
+ s.use(c.Date, c.Model, c.Tokens)
}
}
+ s.Droid = &c
return s
}
+// droidLineOf counts one line of a transcript.
+func droidLineOf(s *state, c *droidCarry, b []byte, main bool) {
+ var l droidLine
+ if json.Unmarshal(b, &l) != nil {
+ return
+ }
+ if !c.Started {
+ c.Started = true
+ var st droidStart
+ if l.Type == "session_start" && json.Unmarshal(b, &st) == nil {
+ s.ID, s.Cwd, s.Named = st.ID, st.Cwd, title(st.Title)
+ if s.Named == "New Session" {
+ s.Named = "" // Droid's until it names one
+ }
+ if st.LastCwd != "" {
+ s.Cwd = st.LastCwd
+ }
+ // a fork's lines up to Upto are copies of its source's
+ c.Upto, c.Copied = st.Forked, st.Forked != ""
+ }
+ return
+ }
+ if l.Type != "message" {
+ return
+ }
+ if c.Copied {
+ if l.ID == c.Upto {
+ c.Copied = false
+ }
+ return
+ }
+ at := tsAt(b, false)
+ s.saw(at, main)
+ switch l.Message.Role {
+ case "user":
+ text := droidText(l.Message.Content)
+ if strings.TrimSpace(text) == "" || !main {
+ return
+ }
+ if strings.HasPrefix(strings.TrimSpace(text), "<") {
+ if s.First == "" {
+ s.First = untagged(text)
+ }
+ return
+ }
+ s.day(dateOf(at)).Prompts++
+ if s.Title == "" {
+ s.Title = title(text)
+ }
+ case "assistant":
+ if main {
+ s.day(dateOf(at)).Replies++
+ }
+ for _, name := range droidTools(l.Message.Content) {
+ s.tool(at, name, "")
+ }
+ }
+}
+
// droidSlot is the tail droid gives a custom model's id made of its name
// ("custom:Kimi-K2-[Groq]-0").
var droidSlot = regexp.MustCompile(`(-\[[^\]]*\])?-\d+$`)
diff --git a/internal/sessions/grow_test.go b/internal/sessions/grow_test.go
index 46bb38fa2..68d17c93e 100644
--- a/internal/sessions/grow_test.go
+++ b/internal/sessions/grow_test.go
@@ -180,3 +180,56 @@ func TestGrokPromptAcrossReads(t *testing.T) {
t.Fatalf("title %q", g.old.Title)
}
}
+
+// Droid appends to a transcript and keeps a running total in its
+// settings: a message appended is read alone, and the total, counted on
+// the day of the last message, moves with it instead of being counted twice.
+func TestDroidReadsOnlyWhatWasAppended(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ putDroid(t, home)
+ dir := filepath.Join(home, ".factory", "sessions", "-work-droid")
+ g := &grower{t: t, files: droidFiles, path: filepath.Join(dir, droidMain+".jsonl")}
+ msg := `{"type":"message","id":"m5","timestamp":"2026-09-27T08:03:00.000Z","message":{"role":"assistant","content":[{"type":"text","text":"Fixed"},{"type":"tool_use","id":"t2","name":"Edit","input":{}}]}}` + "\n"
+ checkGrows(t, g, msg)
+
+ // a day later, the total grown: counted once, on the new day
+ g.read()
+ was := g.size()
+ g.append(`{"type":"message","id":"m6","timestamp":"2026-09-28T09:00:00.000Z","message":{"role":"user","content":"and the logout one"}}` + "\n")
+ os.WriteFile(filepath.Join(dir, droidMain+".settings.json"), []byte(`{"model":"claude-opus-4-5","tokenUsage":{"inputTokens":300,"outputTokens":80,"cacheCreationTokens":10,"cacheReadTokens":2000}}`), 0o644)
+ if at := g.read(); at != was {
+ t.Fatalf("read from %d, not %d", at, was)
+ }
+ if d := g.old.Days["2026-09-27"]; d == nil || len(d.Models) != 0 {
+ t.Fatalf("the total is still on the day before: %+v", d)
+ }
+ if m := g.old.Days["2026-09-28"].Models["claude-opus-4-5"]; m.Input != 300 {
+ t.Fatalf("the total on its day: %+v", g.old.Days["2026-09-28"])
+ }
+ // only the settings changed: nothing of the transcript read
+ os.WriteFile(filepath.Join(dir, droidMain+".settings.json"), []byte(`{"model":"claude-opus-4-5","tokenUsage":{"inputTokens":400,"outputTokens":90}}`), 0o644)
+ if at, end := g.read(), g.size(); at != end {
+ t.Fatalf("settings changed: read from %d of %d", at, end)
+ }
+}
+
+// A fork's copied lines that end in a later read are still passed over.
+func TestDroidForkAcrossReads(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ putDroid(t, home)
+ p := filepath.Join(home, ".factory", "sessions", "-work-droid", droidFork+".jsonl")
+ b, _ := os.ReadFile(p)
+ lines := bytes.SplitAfter(b, []byte("\n"))
+ os.WriteFile(p, bytes.Join(lines[:3], nil), 0o644)
+ g := &grower{t: t, files: droidFiles, path: p}
+ g.read()
+ for _, l := range lines[3:] {
+ g.append(string(l))
+ g.read()
+ }
+ if g.old.Title != "Try another way" {
+ t.Fatalf("title %q", g.old.Title)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 8e1be82df..46cc3fe38 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -213,6 +213,9 @@ type state struct {
// Grok Build: where its updates were read to, and what the read
// carries on with
Grok *grokCarry `json:"grok,omitempty"`
+ // Droid: where its transcript was read to, and what the read carries
+ // on with
+ Droid *droidCarry `json:"droid,omitempty"`
}
// day is one local date's share of a file: tokens by model, and the time
@@ -1198,7 +1201,7 @@ func parserFor(agent string) sessionParser {
case "kimi":
return sessionParser{whole: parseKimi}
case "droid":
- return sessionParser{whole: parseDroid}
+ return sessionParser{grow: parseDroid}
case "cursor":
return sessionParser{whole: parseCursor}
case "codex":
diff --git a/internal/sessions/tail.go b/internal/sessions/tail.go
index 8c6cbbc98..3d717bcf0 100644
--- a/internal/sessions/tail.go
+++ b/internal/sessions/tail.go
@@ -6,7 +6,7 @@ import "os"
// 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
+// and Droid'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.
//
@@ -48,3 +48,30 @@ func (t *tail) holds(path string) bool {
}
return sameHead(headOf(path), t.Head, t.HeadSize) && prefixHash(path, t.Size) == t.Hash
}
+
+// dropUse takes back what use counted, leaving no model or day it made
+// with nothing in it: a summary read on from where it was is then the one
+// a whole read makes.
+func (s *state) dropUse(date, model string, t Tokens) {
+ if t.zero() {
+ return
+ }
+ s.unuse(date, model, t)
+ if m, ok := s.Models[model]; ok && m.zero() {
+ delete(s.Models, model)
+ if len(s.Models) == 0 {
+ s.Models = nil
+ }
+ }
+ if d := s.Days[date]; d != nil {
+ if m, ok := d.Models[model]; ok && m.zero() {
+ delete(d.Models, model)
+ if len(d.Models) == 0 {
+ d.Models = nil
+ }
+ }
+ if len(d.Models) == 0 && d.Active == 0 && d.Hours == nil && d.Prompts == 0 && d.Replies == 0 && d.Tools == nil && d.Skills == nil {
+ delete(s.Days, date)
+ }
+ }
+}