Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a look at the routing history reads today's file only past what it counted, and parses only its latest routes (#3383)
yetone4753a6cfbbdd
diff --git a/internal/gateway/history.go b/internal/gateway/history.goindex bdde77662..372403243 100644--- a/internal/gateway/history.go+++ b/internal/gateway/history.go@@ -62,6 +62,13 @@ var history struct {type counted struct {size int64n int+ // a day's .jsonl, still written to: the bytes tallied, up to the end+ // of its last whole line, and where each of its lines starts, so a+ // look reads only what was added since and parses only the latest+ // lines (#3383: a busy day's file is 350 MB and was read whole at every+ // look, every 15s while the Routing view was open)+ off int64+ starts []int64}const dayForm = "2006-01-02"@@ -270,27 +277,43 @@ func History(day string) (days []HistoryDay, routes []Route, cut bool) {if history.counts == nil {history.counts = map[string]counted{}}+ total := 0for _, d := range historyFiles(HistoryDir()) {- // a day is read only when it is the one asked for, or it has+ // a .gz is read only when it is the one asked for, or it has// changed since it was counted: thirty days gzipped are not read- // again at every look+ // again at every look; a .jsonl only past what was talliedc, ok := history.counts[d.path]- if d.day == day || !ok || c.size != d.size {- n := 0- readDay(d.path, func(line []byte) bool {- n++- if d.day != day {- return true+ if !strings.HasSuffix(d.path, ".gz") {+ c = tally(d, c)+ if d.day == day {+ // the lines before the latest historyMax aren't parsed: a+ // route is written as it is done, so the file is nearly in+ // time order, and historySlack more cover one done late+ from := int64(0)+ if k := len(c.starts) - historyMax - historySlack; k > 0 {+ from = c.starts[k]}- var r Route- if json.Unmarshal(line, &r) == nil {- r.routedAgain()- routes = append(routes, r)+ readDayFrom(d.path, from, func(line []byte) bool {+ routes = appendRoute(routes, line)+ return true+ })+ }+ } else if d.day == day || !ok || c.size != d.size {+ n, skip := 0, -1+ if d.day == day && ok && c.size == d.size {+ skip = c.n - historyMax - historySlack+ }+ readDay(d.path, func(line []byte) bool {+ if n++; d.day == day && n > skip {+ routes = appendRoute(routes, line)}return true})- c = counted{d.size, n}- history.counts[d.path] = c+ c = counted{size: d.size, n: n}+ }+ history.counts[d.path] = c+ if d.day == day {+ total += c.n}if n := c.n; n > 0 {if len(days) > 0 && days[0].Day == d.day {@@ -302,9 +325,72 @@ func History(day string) (days []HistoryDay, routes []Route, cut bool) {}sort.SliceStable(routes, func(i, j int) bool { return routes[i].Time.Before(routes[j].Time) })if len(routes) > historyMax {- routes, cut = routes[len(routes)-historyMax:], true+ routes = routes[len(routes)-historyMax:]+ }+ return days, routes, cut || total > historyMax+}++// historySlack: the lines read past a day's latest historyMax, for a+// route done after the later ones began.+const historySlack = 200++func appendRoute(routes []Route, line []byte) []Route {+ var r Route+ if json.Unmarshal(line, &r) != nil {+ return routes+ }+ r.routedAgain()+ return append(routes, r)+}++// tally counts a day's .jsonl past what c has tallied: only its whole+// lines, a last one without its newline yet maybe half written. A file+// smaller than what was tallied was written over, and is counted again+// from its start.+func tally(d dayFile, c counted) counted {+ if d.size < c.off || c.size > d.size {+ c = counted{}+ }+ if d.size == c.size {+ return c}- return days, routes, cut+ f, err := os.Open(d.path)+ if err != nil {+ return counted{size: d.size}+ }+ defer f.Close()+ if _, err := f.Seek(c.off, io.SeekStart); err != nil {+ return counted{size: d.size}+ }+ buf := make([]byte, 256<<10)+ at, start, full := c.off, c.off, false+ tail := false+ for {+ k, err := f.Read(buf)+ for i, b := range buf[:k] {+ switch b {+ case '\n':+ if full {+ c.starts = append(c.starts, start)+ }+ start, full = at+int64(i)+1, false+ c.off = start+ case ' ', '\t', '\r':+ default:+ full = true+ }+ }+ at += int64(k)+ if err != nil {+ tail = full+ break+ }+ }+ c.size, c.n = d.size, len(c.starts)+ if tail {+ c.n++ // counted as a line, as readDay reads it, but tallied again+ }+ return c}// routedAgain reads a route kept before Routed was: a try that asked@@ -332,12 +418,21 @@ func (r *Route) routedAgain() {}// readDay calls f with each line of a day's file.-func readDay(path string, f func([]byte) bool) {+func readDay(path string, f func([]byte) bool) { readDayFrom(path, 0, f) }++// readDayFrom calls f with each line of a day's file from byte off of a+// .jsonl, a line's start.+func readDayFrom(path string, off int64, f func([]byte) bool) {file, err := os.Open(path)if err != nil {return}defer file.Close()+ if off > 0 {+ if _, err := file.Seek(off, io.SeekStart); err != nil {+ return+ }+ }var r io.Reader = fileif strings.HasSuffix(path, ".gz") {z, err := gzip.NewReader(file)diff --git a/internal/gateway/history_test.go b/internal/gateway/history_test.goindex 826a6aff0..45ff95823 100644--- a/internal/gateway/history_test.go+++ b/internal/gateway/history_test.go@@ -478,3 +478,75 @@ func TestNoRouteWrittenInATestSandbox(t *testing.T) {history.mu.Unlock()})}++// A look at the history reads a day's .jsonl only past what it tallied+// before, and parses only the asked day's latest lines: a busy day's file+// is 350 MB, and the Routing view looks every 15s (#3383). The routes+// tallied are blanked in place behind its back, so a look that read them+// again would count fewer.+func TestHistoryReadsOnlyWhatWasAdded(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())+ day, dir := time.Now().Format(dayForm), HistoryDir()+ path := filepath.Join(dir, day+".jsonl")+ os.MkdirAll(dir, 0o700)+ at := time.Now().Add(-time.Hour)+ line := func(id int) string {+ b, _ := json.Marshal(Route{ID: int64(id), Model: "m", Time: at.Add(time.Duration(id) * time.Millisecond)})+ return string(b) + "\n"+ }+ var b strings.Builder+ n := historyMax + historySlack + 50+ for i := 1; i <= n; i++ {+ b.WriteString(line(i))+ }+ os.WriteFile(path, []byte(b.String()), 0o600)+ if days, rs, cut := History(day); len(days) != 1 || days[0].Requests != n || len(rs) != historyMax || !cut || rs[0].ID != int64(n-historyMax+1) {+ t.Fatalf("days %+v, %d routes from %v, cut %v", days, len(rs), rs[0].ID, cut)+ }+ blank := bytes.Repeat([]byte(" "), len(line(1))-1)+ f, _ := os.OpenFile(path, os.O_WRONLY, 0)+ f.WriteAt(blank, 0)+ // a route added, and one being written+ f.WriteAt([]byte(line(n+1)+line(n + 2)[:20]), int64(b.Len()))+ f.Close()+ days, _, _ := History("")+ if len(days) != 1 || days[0].Requests != n+2 {+ t.Fatalf("days %+v, want %d requests", days, n+2)+ }+ f, _ = os.OpenFile(path, os.O_WRONLY, 0)+ f.WriteAt([]byte(line(n+2)), int64(b.Len()+len(line(n+1))))+ f.Close()+ days, rs, _ := History(day)+ if len(days) != 1 || days[0].Requests != n+2 || len(rs) != historyMax || rs[len(rs)-1].ID != int64(n+2) {+ t.Fatalf("days %+v, %d routes, last %d", days, len(rs), rs[len(rs)-1].ID)+ }+ // written over shorter: counted again from its start+ os.WriteFile(path, []byte(line(1)), 0o600)+ if days, rs, cut := History(day); len(days) != 1 || days[0].Requests != 1 || len(rs) != 1 || cut {+ t.Fatalf("written over: days %+v, %d routes, cut %v", days, len(rs), cut)+ }+}++// BenchmarkHistoryLook is the Routing view's look with no day, every 15s,+// at a busy day's file: 20k routes, one more since the last look.+func BenchmarkHistoryLook(b *testing.B) {+ b.Setenv("XDG_CONFIG_HOME", b.TempDir())+ day, dir := time.Now().Format(dayForm), HistoryDir()+ path := filepath.Join(dir, day+".jsonl")+ os.MkdirAll(dir, 0o700)+ r := Route{ID: 1, Model: "m", Time: time.Now(), Order: make([]Weighed, 40)}+ for i := range r.Order {+ r.Order[i] = Weighed{ID: "acct-" + strconv.Itoa(i), Provider: "codex", Name: "Codex", Icon: "/icons/codex.svg", Model: "gpt-5.5"}+ }+ one, _ := json.Marshal(r)+ one = append(one, '\n')+ os.WriteFile(path, bytes.Repeat(one, 20000), 0o600)+ History("")+ f, _ := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0)+ defer f.Close()+ b.ResetTimer()+ for b.Loop() {+ f.Write(one)+ History("")+ }+}