Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: a Kimi Code wire file read for calls is read on, not whole
yetone11b42c0e2e74
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.goindex 5f79a8b9c..d714ed1d0 100644--- a/internal/sessions/call_cache.go+++ b/internal/sessions/call_cache.go@@ -176,7 +176,8 @@ func keepCalls(path string, st *callFile) {// wholeCallReaders read an agent's calls from one session file at a time,// rather than a line at a time: its calls are not lines, or its file cannot// be read on from the middle of one. Every agent in callSources that is not-// in callFiles needs an entry here, or its calls are never read.+// in callFiles needs an entry here or in lineCallReaders, or its calls are+// never read.var wholeCallReaders = map[string]func(file) (*callFile, bool){"opencode": readOpenCodeCalls,"zcode": readZCodeCalls,@@ -185,12 +186,27 @@ var wholeCallReaders = map[string]func(file) (*callFile, bool){"codebuddy": readWorkBuddyCalls, // #1779// #1605"minimax-code": readMiniMaxCalls,- "kimi": readKimiCalls, // #1607- "pi": readPiCalls, // #1951+ "pi": readPiCalls, // #1951"omp": readPiCalls,"agy": readAgyCalls, // #2097}+// lineCallReader reads an agent's calls a line at a time, as Claude Code's+// and Codex's are: a file that only grew is read on from where the last read+// left off (prepareCalls), and only the calls it adds are written to the+// disk shard. begin starts a file read from its start; line reads one whole+// line of it. Nothing a line leaves for the next may live outside st.+type lineCallReader struct {+ begin func(st *callFile, f file)+ line func(st *callFile, b []byte)+}++// lineCallReaders are the agents in callSources, beside callFiles' own,+// whose calls are each one line of a file only appended to.+var lineCallReaders = map[string]lineCallReader{+ "kimi": {kimiCallsBegin, kimiCallLine}, // #1607+}+func readCalls(f file) *callFile {h := fnv.New32a()_, _ = h.Write([]byte(f.path))@@ -247,18 +263,26 @@ func readCalls(f file) *callFile {if continued {start = len(old.Calls)}+ line := claudeCallLine+ if f.agent == "codex" {+ line = codexCallLine+ } else if r, ok := lineCallReaders[f.agent]; ok {+ if st.Off == 0 {+ r.begin(st, f)+ }+ line = r.line+ }off, err := scanAt(f.path, st.Off, func(b []byte) bool { return callHead(st, b) }, func(b []byte, start, end int64) bool {st.At, st.End = start, end- if f.agent == "codex" {- codexCallLine(st, b)- } else {- claudeCallLine(st, b)- }+ line(st, b)return true})if err == nil {st.Off, st.Size, st.Mod = off, f.size, f.mod.UnixNano()- st.ContentHash = prefixHash(f.path, f.size)+ // the bytes read, not the listed size: a listing may fold another+ // file's size into this one's (Kimi's state.json), and hashing past+ // the end would leave no hash to read on from+ st.ContentHash = prefixHash(f.path, off)writeCalls(st, start, continued)}// Do not turn a one-time historical scan into an indefinitely retaineddiff --git a/internal/sessions/calls.go b/internal/sessions/calls.goindex 7c640bc6f..c6b1aafc4 100644--- a/internal/sessions/calls.go+++ b/internal/sessions/calls.go@@ -306,15 +306,15 @@ func callFiles() []file {return out}-// callSources are callFiles and the agents whose calls are read whole-// rather than line by line: OpenCode's (#680) and ZCode's, whose calls are-// rows of their database or their JSON files; DeepSeek Harness's, whose-// session file is packed in frames and so cannot be read on from the middle-// of one; and WorkBuddy's, whose usage lines repeat a reply's id as it goes-// on, as CodeBuddy Code's do (#1779); and MiniMax Code's, whose messages-// file a later generation of its history may replace (#1605); and Kimi-// Code's, whose forks copy their parent's records (#1607). Every agent here-// needs an entry in wholeCallReaders.+// callSources are callFiles and the other agents' session files. Most are+// read whole rather than line by line: OpenCode's (#680) and ZCode's, whose+// calls are rows of their database or their JSON files; DeepSeek Harness's,+// whose session file is packed in frames and so cannot be read on from the+// middle of one; and WorkBuddy's, whose usage lines repeat a reply's id as it+// goes on, as CodeBuddy Code's do (#1779); and MiniMax Code's, whose messages+// file a later generation of its history may replace (#1605). Kimi Code's+// (#1607) are lines of a file only appended to, read on as callFiles' are.+// Every agent here needs an entry in wholeCallReaders or lineCallReaders.func callSources() []file {out := callFiles()out = append(out, openCodeCallFiles()...)@@ -365,7 +365,7 @@ func prepareCalls(f file, old *callFile) *callFile {head := headOf(f.path)var st *callFile- if old != nil && !packed(f.path) && f.size >= old.Size && old.Off <= f.size && sameHead(head, old.Head, old.HeadSize) && old.ContentHash != "" && prefixHash(f.path, old.Size) == old.ContentHash {+ if old != nil && !packed(f.path) && f.size >= old.Size && old.Off <= f.size && sameHead(head, old.Head, old.HeadSize) && old.ContentHash != "" && prefixHash(f.path, old.Off) == old.ContentHash {st = old.clone()} else {st = &callFile{Agent: f.agent}diff --git a/internal/sessions/calls_kimi.go b/internal/sessions/calls_kimi.goindex d0c1c45ce..a242b2f03 100644--- a/internal/sessions/calls_kimi.go+++ b/internal/sessions/calls_kimi.go@@ -1,12 +1,9 @@package sessionsimport (- "bufio""bytes""cmp""encoding/json"- "errors"- "io""os""path/filepath""strconv"@@ -88,35 +85,27 @@ type kimiRecord struct {var kimiUsageRecord = []byte(`"usage.record"`)-// readKimiCalls reads one agent's wire file whole.-func readKimiCalls(f file) (*callFile, bool) {- r, err := os.Open(f.path)- if err != nil {- return &callFile{Path: f.path, Agent: "kimi"}, false+// kimiCallsBegin starts an agent's wire file read from its start. Kimi Code+// only appends to it, each record a line of its own, so readCalls reads one+// that grew on from where it left off (lineCallReaders).+func kimiCallsBegin(st *callFile, f file) {+ st.Session = strings.TrimPrefix(f.key, "kimi:")+}++// kimiCallLine reads one line of an agent's wire file: a usage.record is a+// call.+func kimiCallLine(st *callFile, b []byte) {+ b = bytes.TrimSpace(b)+ if len(b) == 0 || len(b) > maxLine || !bytes.Contains(b, kimiUsageRecord) {+ return+ }+ var rec kimiRecord+ if json.Unmarshal(b, &rec) != nil {+ return}- defer r.Close()- agentDir := filepath.Base(filepath.Dir(f.path))- st := &callFile{Agent: "kimi", Path: f.path, Session: strings.TrimPrefix(f.key, "kimi:")}- br := bufio.NewReaderSize(r, 1<<20)- 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, kimiUsageRecord) {- var rec kimiRecord- if json.Unmarshal(b, &rec) == nil {- if c, ok := kimiCall(st, agentDir, rec); ok {- st.Calls = append(st.Calls, c)- }- }- }- if err != nil {- break- }+ if c, ok := kimiCall(st, filepath.Base(filepath.Dir(st.Path)), rec); ok {+ st.Calls = append(st.Calls, c)}- return st, true}// kimiCall is a usage.record as a call.diff --git a/internal/sessions/calls_kimi_test.go b/internal/sessions/calls_kimi_test.goindex b7cd7b0bf..70cfc6e98 100644--- a/internal/sessions/calls_kimi_test.go+++ b/internal/sessions/calls_kimi_test.go@@ -4,7 +4,10 @@ import ("bytes""os""path/filepath"+ "slices"+ "strconv""strings"+ "sync""testing""time")@@ -175,8 +178,8 @@ func TestKimiCodeForkCountsOnce(t *testing.T) {// and two subagents of one session whose 1.5 records name themselves:// the same call in two agents' files is two callssub := strings.Replace(kimiRecordLine, `"agentId":"main"`, `"agentId":"agent-0"`, 1)- writeKimiWire(t, home, "session_agents", "agent-0", sub)- writeKimiWire(t, home, "session_agents", "main", kimiRecordLine)+ writeKimiWire(t, home, "session_agents", "agent-0", sub, "")+ writeKimiWire(t, home, "session_agents", "main", kimiRecordLine, "")tot := kimiTotals(kimiCalls())if got := tot["session_169766c9-86f5-4577-97a3-a51d402db5b9"]; got != kimiWant["session_169766c9-86f5-4577-97a3-a51d402db5b9"] {@@ -232,3 +235,89 @@ func TestKimiCodeBrokenFiles(t *testing.T) {}}}++// A Kimi Code wire file that grew is read on from where the last read of+// its calls left off, not whole again; a half-written last line waits for+// the rest; a file cut shorter or written over is read whole again. Before+// this, every poll after an append decoded the whole file again.+func TestKimiCodeCallsReadOnlyWhatWasAppended(t *testing.T) {+ home := kimiHome(t)+ rec := func(i int) string {+ r := strings.Replace(kimiRecordLine, `1791696952640`, strconv.FormatInt(1791696900000+int64(i)*1000, 10), 1)+ return strings.Replace(r, `"inputOther":20257`, `"inputOther":`+strconv.Itoa(20000+i), 1)+ }+ writeKimiWire(t, home, "session_grow", "main", rec(1), rec(2), rec(3), "")+ path := filepath.Join(home, "sessions", "wd_test_000000000009", "session_grow", "agents", "main", "wire.jsonl")+ // the listing folds state.json's size into the main wire file's+ if err := os.WriteFile(filepath.Join(home, "sessions", "wd_test_000000000009", "session_grow", "state.json"), []byte(`{"title":"Grow","cwd":"/work/grow"}`), 0o644); err != nil {+ t.Fatal(err)+ }+ var mu sync.Mutex+ var starts []int64+ scanHook = func(p string, off int64) {+ if p == path {+ mu.Lock()+ starts = append(starts, off)+ mu.Unlock()+ }+ }+ t.Cleanup(func() { scanHook = nil })+ read := func(what string, from int64, want ...int) {+ t.Helper()+ mu.Lock()+ starts = nil+ mu.Unlock()+ var calls []Call+ for _, s := range CallSources() {+ if s.Path == path {+ calls = ReadCallSource(s)+ }+ }+ mu.Lock()+ got := append([]int64(nil), starts...)+ mu.Unlock()+ if len(got) == 0 {+ t.Fatalf("%s: not read", what)+ }+ for _, off := range got {+ if off != from {+ t.Fatalf("%s: read from %d, want %d (%v)", what, off, from, got)+ }+ }+ var in []int+ for _, c := range calls {+ in = append(in, c.Input-20000)+ }+ if !slices.Equal(in, want) {+ t.Fatalf("%s: calls %v, want %v", what, in, want)+ }+ }+ size := func() int64 {+ t.Helper()+ st, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ return st.Size()+ }+ read("first read", 0, 1, 2, 3)++ end := size()+ appendText(t, path, rec(4)+"\n"+rec(5)[:40])+ read("appended, half a line after", end, 1, 2, 3, 4)++ appendText(t, path, rec(5)[40:]+"\n")+ read("the half line finished", end+int64(len(rec(4))+1), 1, 2, 3, 4, 5)++ // cut shorter: read whole again+ writeKimiWire(t, home, "session_grow", "main", rec(7), "")+ read("cut shorter", 0, 7)++ // written over, longer than it was: read whole again+ writeKimiWire(t, home, "session_grow", "main", rec(8), rec(9), "")+ later := time.Now().Add(time.Hour)+ if err := os.Chtimes(path, later, later); err != nil {+ t.Fatal(err)+ }+ read("written over", 0, 8, 9)+}