Skip to content

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

sessions: a Pi or omp session file read for calls is read on, not whole

yetoneda9fc17eb6c6
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go
index d714ed1d0..4127f1c86 100644
--- a/internal/sessions/call_cache.go
+++ b/internal/sessions/call_cache.go
@@ -186,8 +186,6 @@ var wholeCallReaders = map[string]func(file) (*callFile, bool){
"codebuddy": readWorkBuddyCalls, // #1779
// #1605
"minimax-code": readMiniMaxCalls,
- "pi": readPiCalls, // #1951
- "omp": readPiCalls,
"agy": readAgyCalls, // #2097
}
@@ -195,16 +193,22 @@ var wholeCallReaders = map[string]func(file) (*callFile, bool){
// 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.
+// line of it. Nothing a line leaves for the next may live outside st. kept,
+// when a reader carries something from line to line, says whether st holds
+// it: a read kept on disk before it was carried there does not, and is
+// read whole again.
type lineCallReader struct {
begin func(st *callFile, f file)
line func(st *callFile, b []byte)
+ kept func(st *callFile) bool
}
// 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
+ "kimi": {kimiCallsBegin, kimiCallLine, nil}, // #1607
+ "pi": {piCallsBegin, readPiCallLine, piCallsKept}, // #1951
+ "omp": {piCallsBegin, readPiCallLine, piCallsKept},
}
func readCalls(f file) *callFile {
@@ -257,6 +261,9 @@ func readCalls(f file) *callFile {
callsMu.Unlock()
return st
}
+ if r, ok := lineCallReaders[f.agent]; ok && r.kept != nil && old != nil && !r.kept(old) {
+ old = nil
+ }
st := prepareCalls(f, old)
continued := old != nil && st.Off == old.Off && st.Off > 0
start := 0
@@ -325,16 +332,30 @@ type callDiskState struct {
At, End, AsstEnd int64
Session, Requested string
LastUser, LastAsst time.Time
+ // Pi's and omp's carry (piCarry), flat: PiOn when there is one
+ PiOn, PiHeader bool
+ PiCwd string
+ PiSince time.Time
+ PiProvider, PiModel string
}
func diskCallState(s *callFile) callDiskState {
- return callDiskState{s.ContentHash, s.Size, s.Mod, s.Off, s.Head, s.HeadSize,
- s.Path, s.Agent, s.At, s.End, s.AsstEnd, s.Session, s.Requested, s.LastUser, s.LastAsst}
+ d := callDiskState{ContentHash: s.ContentHash, Size: s.Size, Mod: s.Mod, Off: s.Off, Head: s.Head, HeadSize: s.HeadSize,
+ Path: s.Path, Agent: s.Agent, At: s.At, End: s.End, AsstEnd: s.AsstEnd, Session: s.Session, Requested: s.Requested,
+ LastUser: s.LastUser, LastAsst: s.LastAsst}
+ if p := s.Pi; p != nil {
+ d.PiOn, d.PiHeader, d.PiCwd, d.PiSince, d.PiProvider, d.PiModel = true, p.Header, p.Cwd, p.Since, p.Provider, p.Model
+ }
+ return d
}
func (s callDiskState) restore() *callFile {
- return &callFile{ContentHash: s.ContentHash, Size: s.Size, Mod: s.Mod, Off: s.Off,
+ st := &callFile{ContentHash: s.ContentHash, Size: s.Size, Mod: s.Mod, Off: s.Off,
Head: s.Head, HeadSize: s.HeadSize, Path: s.Path, Agent: s.Agent, At: s.At, End: s.End,
AsstEnd: s.AsstEnd, Session: s.Session, Requested: s.Requested, LastUser: s.LastUser, LastAsst: s.LastAsst}
+ if s.PiOn {
+ st.Pi = &piCarry{s.PiHeader, s.PiCwd, s.PiSince, s.PiProvider, s.PiModel}
+ }
+ return st
}
func callText(c *Call) [13]*string {
diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go
index c6b1aafc4..87ecfbdaa 100644
--- a/internal/sessions/calls.go
+++ b/internal/sessions/calls.go
@@ -94,6 +94,8 @@ type callFile struct {
CompactMs int64
// Codex
CX *cxRun
+ // Pi and omp
+ Pi *piCarry
}
// cxRun is where a rollout's read stands: what its lines have said so far.
@@ -313,7 +315,8 @@ func callFiles() []file {
// 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.
+// (#1607), Pi's and omp's (#1951) 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()
@@ -397,6 +400,10 @@ func (st *callFile) clone() *callFile {
r.Timings = append([]cxCallTiming(nil), r.Timings...)
c.CX = &r
}
+ if st.Pi != nil {
+ p := *st.Pi
+ c.Pi = &p
+ }
return &c
}
diff --git a/internal/sessions/calls_pi.go b/internal/sessions/calls_pi.go
index 1a462fdae..a9e161f90 100644
--- a/internal/sessions/calls_pi.go
+++ b/internal/sessions/calls_pi.go
@@ -1,13 +1,9 @@
package sessions
import (
- "bufio"
"bytes"
"encoding/json"
- "errors"
- "io"
"math"
- "os"
"strings"
"time"
)
@@ -73,59 +69,65 @@ type piCallLine struct {
} `json:"message"`
}
-// readPiCalls reads one Pi or omp session file whole.
-func readPiCalls(f file) (*callFile, bool) {
- r, err := os.Open(f.path)
- if err != nil {
- return &callFile{Path: f.path, Agent: f.agent}, false
+// piCarry is what a Pi or omp session file's lines have said so far that a
+// later line's call is read with: the header's cwd and, for a fork, when it
+// began (what was written before is its parent's), and the provider and
+// model in use. Pi and omp only append to the file, a line an entry, so a
+// file that grew is read on from where the last read left off with it
+// (lineCallReaders).
+type piCarry struct {
+ Header bool
+ Cwd string
+ Since time.Time
+ Provider, Model string
+}
+
+// piCallsBegin starts a session file read from its start.
+func piCallsBegin(st *callFile, f file) {
+ st.Session = strings.TrimPrefix(f.key, f.agent+":")
+ st.Pi = &piCarry{}
+}
+
+// piCallsKept is whether st holds what reading on needs: a disk shard
+// written before the carry was kept there does not, and its file is read
+// whole again once it changes.
+func piCallsKept(st *callFile) bool { return st.Pi != nil }
+
+// readPiCallLine reads one line of a Pi or omp session file.
+func readPiCallLine(st *callFile, b []byte) {
+ if len(b) == 0 || len(b) > maxLine || !(bytes.Contains(b, piUsed) || bytes.Contains(b, piHeader) || bytes.Contains(b, piModel)) {
+ return
}
- defer r.Close()
- st := &callFile{Agent: f.agent, Path: f.path, Session: strings.TrimPrefix(f.key, f.agent+":")}
- var cwd, provider, model string
- var header bool
- var since time.Time // a fork's: what was written before it is its parent's
- 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, piUsed) || bytes.Contains(b, piHeader) || bytes.Contains(b, piModel)) {
- var l piCallLine
- if json.Unmarshal(b, &l) == nil {
- switch l.Type {
- case "session":
- if !header {
- header, cwd = true, l.Cwd
- if l.Parent != "" {
- since, _ = time.Parse(time.RFC3339Nano, l.Timestamp)
- }
- }
- case "model_change":
- p, id := l.Provider, l.ModelID
- if id == "" {
- p, id, _ = strings.Cut(l.Model, "/")
- }
- if id != "" {
- provider, model = p, id
- }
- case "message":
- if m := l.Message; m != nil && m.Role == "assistant" && m.Model != "" {
- provider, model = m.Provider, m.Model
- }
- }
- if c, ok := piCall(st, f.agent, l, provider, model); ok && !c.Time.Before(since) {
- c.Cwd = st.str(cwd)
- st.Calls = append(st.Calls, c)
- }
+ var l piCallLine
+ if json.Unmarshal(b, &l) != nil {
+ return
+ }
+ p := st.Pi
+ switch l.Type {
+ case "session":
+ if !p.Header {
+ p.Header, p.Cwd = true, l.Cwd
+ if l.Parent != "" {
+ p.Since, _ = time.Parse(time.RFC3339Nano, l.Timestamp)
}
}
- if err != nil {
- break
+ case "model_change":
+ prov, id := l.Provider, l.ModelID
+ if id == "" {
+ prov, id, _ = strings.Cut(l.Model, "/")
+ }
+ if id != "" {
+ p.Provider, p.Model = prov, id
}
+ case "message":
+ if m := l.Message; m != nil && m.Role == "assistant" && m.Model != "" {
+ p.Provider, p.Model = m.Provider, m.Model
+ }
+ }
+ if c, ok := piCall(st, st.Agent, l, p.Provider, p.Model); ok && !c.Time.Before(p.Since) {
+ c.Cwd = st.str(p.Cwd)
+ st.Calls = append(st.Calls, c)
}
- return st, true
}
// piCall is an entry as a call, on the provider and model in use for the
diff --git a/internal/sessions/calls_pi_test.go b/internal/sessions/calls_pi_test.go
index 7c3c17484..d1785ccc1 100644
--- a/internal/sessions/calls_pi_test.go
+++ b/internal/sessions/calls_pi_test.go
@@ -1,8 +1,12 @@
package sessions
import (
+ "os"
"path/filepath"
+ "slices"
"sort"
+ "strconv"
+ "sync"
"testing"
"time"
)
@@ -57,7 +61,7 @@ func TestPiCalls(t *testing.T) {
// first
for _, f := range piFiles() {
if f.key == "pi:"+fork {
- if st, ok := readPiCalls(f); !ok || len(st.Calls) != 1 {
+ if st := readCalls(f); len(st.Calls) != 1 {
t.Errorf("the fork's own calls: %+v", st.Calls)
}
}
@@ -108,3 +112,116 @@ func TestPiCallsBesideReplies(t *testing.T) {
}
}
}
+
+// A Pi session file that grew is read on from where the last read left
+// off, with what its earlier lines said (its cwd, a fork's start, the
+// model in use) carried on, in memory and in the disk shard; a half
+// line waits for the rest, and a file cut shorter or written over is
+// read whole again.
+func TestPiCallsReadOnlyWhatWasAppended(t *testing.T) {
+ claude, _ := setup(t)
+ dir := filepath.Join(filepath.Dir(claude), "pi", "sessions", "--work-grow--")
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ path := filepath.Join(dir, "2026-10-13T08-00-00-000Z_01a1343b-0000-7000-8000-000000000077.jsonl")
+ t0 := time.Date(2026, 10, 13, 8, 0, 0, 0, time.UTC)
+ // a fork: what was written before its own time is its parent's
+ head := `{"type":"session","version":3,"id":"01a1343b-0000-7000-8000-000000000077","timestamp":"2026-10-13T08:00:00.000Z","cwd":"/work/grow","parentSession":"/p.jsonl"}` + "\n" +
+ `{"type":"model_change","id":"m0","timestamp":"2026-10-13T08:00:00.000Z","provider":"relay","modelId":"deepseek-v4"}` + "\n"
+ // a compaction's usage is on the provider and model in use
+ rec := func(i int) string {
+ at := t0.Add(time.Duration(i) * time.Second).Format("2006-01-02T15:04:05.000Z")
+ return `{"type":"compaction","id":"c` + strconv.Itoa(i+100) + `","timestamp":"` + at + `","usage":{"input":` + strconv.Itoa(20000+i) + `,"output":5,"cacheRead":0,"cacheWrite":0}}`
+ }
+ write := func(lines ...string) {
+ t.Helper()
+ b := head
+ for _, l := range lines {
+ b += l + "\n"
+ }
+ if err := os.WriteFile(path, []byte(b), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ write(rec(-5), rec(1), rec(2))
+ 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 {
+ if c.Model != "deepseek-v4" || c.Upstream != "relay" || c.Cwd != "/work/grow" || c.Agent != "pi" {
+ t.Fatalf("%s: call %+v lost what the earlier lines said", what, c)
+ }
+ 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)
+
+ end := size()
+ appendText(t, path, rec(3)+"\n"+rec(-4)+"\n"+rec(5)[:40])
+ read("appended, half a line after", end, 1, 2, 3)
+
+ end += int64(len(rec(3)) + len(rec(-4)) + 2)
+ appendText(t, path, rec(5)[40:]+"\n")
+ read("the half line finished", end, 1, 2, 3, 5)
+
+ // read again after the in-memory reads are gone: from the disk shard,
+ // with what it carried
+ resetCalls()
+ end = size()
+ appendText(t, path, rec(6)+"\n")
+ read("from the disk shard", end, 1, 2, 3, 5, 6)
+
+ // cut shorter: read whole again
+ write(rec(7))
+ read("cut shorter", 0, 7)
+
+ // written over, longer than it was: read whole again
+ write(rec(8), rec(9), rec(10))
+ later := time.Now().Add(time.Hour)
+ if err := os.Chtimes(path, later, later); err != nil {
+ t.Fatal(err)
+ }
+ read("written over", 0, 8, 9, 10)
+}