Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: a Kimi Code session read before is read on from where it was left, not whole
yetonee03d495f8324
diff --git a/internal/sessions/grow_test.go b/internal/sessions/grow_test.goindex 68d17c93e..1324eef26 100644--- a/internal/sessions/grow_test.go+++ b/internal/sessions/grow_test.go@@ -233,3 +233,57 @@ func TestDroidForkAcrossReads(t *testing.T) {t.Fatalf("title %q", g.old.Title)}}++// Kimi Code appends to an agent's wire.jsonl: a record appended is read+// alone, and a state.json rewritten reads nothing of the wire.+func TestKimiReadsOnlyWhatWasAppended(t *testing.T) {+ inZone(t, 0)+ claude, _ := setup(t)+ dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")+ kimiSessionsIn(t, dir)+ sessions := filepath.Join(dir, "sessions")+ slug := "wd_v14-cancel_000000000003"+ g := &grower{t: t, files: kimiFiles, path: filepath.Join(sessions, slug, kimiCancel, "agents", "main", "wire.jsonl")}+ rec := `{"type":"usage.record","model":"kimi-code/k3","usage":{"inputOther":40,"output":5,"inputCacheRead":900,"inputCacheCreation":0},"usageScope":"turn","time":1784687340000}` + "\n"+ checkGrows(t, g, rec)++ g.read()+ kimiSetState(t, sessions, slug, kimiCancel, map[string]any{"title": "Cancelled twice", "isCustomTitle": true})+ if at, end := g.read(), g.size(); at != end || g.old.Custom != "Cancelled twice" {+ t.Fatalf("state.json changed: read from %d of %d, custom %q", at, end, g.old.Custom)+ }+ // the session now says it was forked: what it counted is read again+ kimiSetState(t, sessions, slug, kimiCancel, map[string]any{"forkedFrom": kimiJune})+ if at := g.read(); at != 0 {+ t.Fatalf("made a fork: read from %d", at)+ }+}++// A fork's marker that comes in a later read still ends its copy of its+// source: what was counted before it is taken back.+func TestKimiForkMarkerAcrossReads(t *testing.T) {+ inZone(t, 0)+ claude, _ := setup(t)+ dir := filepath.Join(filepath.Dir(claude), "home", ".kimi-code")+ kimiSessionsIn(t, dir)+ sessions := filepath.Join(dir, "sessions")+ slug := "wd_v14-cancel_000000000003"+ src, err := os.ReadFile(filepath.Join(sessions, slug, kimiCancel, "agents", "main", "wire.jsonl"))+ if err != nil {+ t.Fatal(err)+ }+ id := "session_f0000000-0000-4000-8000-0000000000aa"+ w := filepath.Join(sessions, slug, id, "agents", "main", "wire.jsonl")+ os.MkdirAll(filepath.Dir(w), 0o755)+ os.WriteFile(w, src, 0o644)+ kimiSetState(t, sessions, slug, id, map[string]any{"cwd": "/w", "createdAt": int64(1784687000000), "title": "Again", "forkedFrom": kimiCancel})+ g := &grower{t: t, files: kimiFiles, path: w}+ g.read()+ g.append(`{"type":"forked","agentId":"main","time":1791700000000}` + "\n")+ g.read()+ g.append(`{"type":"usage.record","model":"kimi-code/k3","usage":{"inputOther":7,"output":3,"inputCacheRead":0,"inputCacheCreation":0},"usageScope":"turn","time":1791700005000}` + "\n")+ g.read()+ if g.old.Models["kimi-code/k3"] != (Tokens{Input: 7, Output: 3}) || len(g.old.Models) != 1 {+ t.Fatalf("the fork's own: %+v", g.old.Models)+ }+}diff --git a/internal/sessions/kimi.go b/internal/sessions/kimi.goindex 4ebcaf1ff..42b76fdd6 100644--- a/internal/sessions/kimi.go+++ b/internal/sessions/kimi.go@@ -196,103 +196,132 @@ var (kimiCounts = [][]byte{kimiUsageRecord, []byte(`"turn.prompt"`), []byte(`"step.end"`), []byte(`"tool.call"`)})-// parseKimi reads one agent's wire file of a session whole, with the-// session's state.json for its own.-func parseKimi(f file) *state {- s := &state{Size: f.size, Mod: f.mod.UnixNano()}- var since time.Time- forked := false- if st, ok := kimiStateOf(f.path); ok {- created := kimiTime(st.CreatedAt)- forked = st.ForkedFrom != ""- if forked {- since = created+// kimiCarry is what a read of a Kimi Code wire file carries on with:+// where it read to, what of state.json its records were counted under,+// and the time before which a fork's records are its source's.+type kimiCarry struct {+ Tail *tail `json:"tail,omitempty"`+ Created time.Time `json:"created,omitzero"`+ Forked bool `json:"forked,omitempty"`+ Since time.Time `json:"since,omitzero"`+}++// parseKimi reads one agent's wire file of a session, with the session's+// state.json for its own. Kimi only appends to a wire file, so one read+// before is read on from where that read left off; state.json, rewritten+// as the session goes on, is read again each time. A file cut short or+// replaced, or a state.json that now says another creation or fork, is+// read whole again.+func parseKimi(f file, old *state) *state {+ var c kimiCarry+ var st kimiState+ hasSt := false+ if st, hasSt = kimiStateOf(f.path); hasSt {+ c.Created = kimiTime(st.CreatedAt)+ c.Forked = st.ForkedFrom != ""+ if c.Forked {+ c.Since = c.Created}- if f.main {- s.Cwd = cmp.Or(st.Cwd, st.WorkDir, kimiIndexedWorkDir(f.path))- s.Custom, s.Named = st.names()- s.Custom, s.Named = title(s.Custom), title(s.Named)- s.saw(created, true)+ }+ fresh := func() *state {+ s := &state{}+ if hasSt && f.main {+ s.saw(c.Created, true)}+ return s}- // a fork's copy of its source ends at its last marker- skip := 0- if forked {- n := 0- scan(f.path, 0, func(b []byte) {- n++- if bytes.Contains(b, kimiForked) {- var r kimiWire- if json.Unmarshal(b, &r) == nil && r.Type == "forked" {- skip, since = n, time.Time{}- }+ var s *state+ var off int64+ if o := old; o != nil && o.Kimi != nil && o.Kimi.Created.Equal(c.Created) && o.Kimi.Forked == c.Forked && o.Kimi.Tail.holds(f.path) {+ s, c, off = o.clone(), *o.Kimi, o.Kimi.Tail.Off+ } else {+ s = fresh()+ }+ end, err := scanAt(f.path, off, nil, func(b []byte, _, _ int64) bool {+ if c.Forked && bytes.Contains(b, kimiForked) {+ var r kimiWire+ if json.Unmarshal(b, &r) == nil && r.Type == "forked" {+ // a fork's copy of its source ends at its last marker:+ // what was counted before it is its source's+ s, c.Since = fresh(), time.Time{}+ return true}- })+ }+ kimiCount(s, b, c.Since, f.main)+ return true+ })+ c.Tail = nil+ if err == nil {+ c.Tail = markTail(f.path, end, 0, end)}- n := 0- scan(f.path, 0, func(b []byte) {- n++- if n <= skip {- return+ s.Size, s.Mod = f.size, f.mod.UnixNano()+ if hasSt && f.main {+ s.Cwd = cmp.Or(st.Cwd, st.WorkDir, kimiIndexedWorkDir(f.path))+ s.Custom, s.Named = st.names()+ s.Custom, s.Named = title(s.Custom), title(s.Named)+ }+ s.Kimi = &c+ return s+}++// kimiCount counts one record of a wire file, none before since.+func kimiCount(s *state, b []byte, since time.Time, main bool) {+ count := false+ for _, k := range kimiCounts {+ if bytes.Contains(b, k) {+ count = true+ break}- count := false- for _, k := range kimiCounts {- if bytes.Contains(b, k) {- count = true- break- }+ }+ var r kimiWire+ if !count {+ // only its time: a light read of the rest+ var t struct {+ Time int64 `json:"time"`}- var r kimiWire- if !count {- // only its time: a light read of the rest- var t struct {- Time int64 `json:"time"`- }- if json.Unmarshal(b, &t) == nil {- if at := ms(t.Time); !at.IsZero() && !at.Before(since) {- s.saw(at, f.main)- }+ if json.Unmarshal(b, &t) == nil {+ if at := ms(t.Time); !at.IsZero() && !at.Before(since) {+ s.saw(at, main)}- return}- if json.Unmarshal(b, &r) != nil {- return+ return+ }+ if json.Unmarshal(b, &r) != nil {+ return+ }+ at := ms(r.Time)+ if at.IsZero() || at.Before(since) {+ return+ }+ s.saw(at, main)+ switch r.Type {+ case "usage.record":+ if u := r.Usage; u != nil && u.InputOther >= 0 && u.Output >= 0 && u.InputCacheRead >= 0 && u.InputCacheCreation >= 0 {+ model := cmp.Or(strings.TrimPrefix(r.Model, kimiGateway), "kimi")+ s.use(dateOf(at), model, Tokens{Input: u.InputOther, Output: u.Output, CacheRead: u.InputCacheRead, CacheWrite: u.InputCacheCreation})}- at := ms(r.Time)- if at.IsZero() || at.Before(since) {+ case "turn.prompt":+ if !main || r.Origin.Kind != "user" {return}- s.saw(at, f.main)- switch r.Type {- case "usage.record":- if u := r.Usage; u != nil && u.InputOther >= 0 && u.Output >= 0 && u.InputCacheRead >= 0 && u.InputCacheCreation >= 0 {- model := cmp.Or(strings.TrimPrefix(r.Model, kimiGateway), "kimi")- s.use(dateOf(at), model, Tokens{Input: u.InputOther, Output: u.Output, CacheRead: u.InputCacheRead, CacheWrite: u.InputCacheCreation})- }- case "turn.prompt":- if !f.main || r.Origin.Kind != "user" {- return- }- s.day(dateOf(at)).Prompts++- if s.Title == "" {- var text []string- for _, p := range r.Input {- if p.Type == "text" {- text = append(text, p.Text)- }+ s.day(dateOf(at)).Prompts+++ if s.Title == "" {+ var text []string+ for _, p := range r.Input {+ if p.Type == "text" {+ text = append(text, p.Text)}- s.Title = title(strings.Join(text, " "))}- case "context.append_loop_event":- switch r.Event.Type {- case "step.end":- if f.main && r.Event.FinishReason == "end_turn" {- s.day(dateOf(at)).Replies++- }- case "tool.call":- s.tool(at, r.Event.Name, "")+ s.Title = title(strings.Join(text, " "))+ }+ case "context.append_loop_event":+ switch r.Event.Type {+ case "step.end":+ if main && r.Event.FinishReason == "end_turn" {+ s.day(dateOf(at)).Replies++}+ case "tool.call":+ s.tool(at, r.Event.Name, "")}- })- return s+ }}diff --git a/internal/sessions/kimi_test.go b/internal/sessions/kimi_test.goindex e4967bc90..53255462e 100644--- a/internal/sessions/kimi_test.go+++ b/internal/sessions/kimi_test.go@@ -187,7 +187,7 @@ func TestKimiCodeSessionCounts(t *testing.T) {}sum := func(id string) (prompts, replies, tools int, custom, named string) {for _, f := range byKey["kimi:"+id] {- s := parseKimi(f)+ s := parseKimi(f, nil)for _, d := range s.Days {prompts += d.Promptsreplies += d.Repliesdiff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.goindex 46cc3fe38..71c81ff0d 100644--- a/internal/sessions/sessions.go+++ b/internal/sessions/sessions.go@@ -216,6 +216,9 @@ type state struct {// Droid: where its transcript was read to, and what the read carries// on withDroid *droidCarry `json:"droid,omitempty"`+ // Kimi Code: where its wire file was read to, and what the read+ // carries on with+ Kimi *kimiCarry `json:"kimi,omitempty"`}// day is one local date's share of a file: tokens by model, and the time@@ -1199,7 +1202,7 @@ func parserFor(agent string) sessionParser {case "grok":return sessionParser{grow: parseGrok}case "kimi":- return sessionParser{whole: parseKimi}+ return sessionParser{grow: parseKimi}case "droid":return sessionParser{grow: parseDroid}case "cursor":diff --git a/internal/sessions/tail.go b/internal/sessions/tail.goindex 3d717bcf0..69544e494 100644--- a/internal/sessions/tail.go+++ b/internal/sessions/tail.go@@ -5,8 +5,8 @@ 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-// and Droid's sessions are kept so). A file that shrank, or whose+// file has only grown, instead of reading it whole again (Grok Build's,+// Droid's and Kimi Code'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.//