A user running magpie on a 1-core, 962 MB server opened Usage › Context.
CPU went to 100%, memory to 91%, and the server went down.
Cause: the tab asks /api/context and /api/tune at every refresh of the
Usage page (every 5s by default). Each ask:
- /api/context read up to 7 days of routing history again, as whole
gateway.Route values (prompt items, order, tries). A gzipped day not yet
counted was parsed whole, every route of it, before being cut to its
latest 2000 (gateway.History).
- /api/tune read every Claude Code and Codex session file in the range
again, whole, into []sessions.Call, then worked the advice out of all of
them.
Now:
- gateway.History counts a .gz day's lines first and then parses only its
latest historyMax+historySlack routes, as it already did for .jsonl.
- /api/context keeps each day's routes slimmed (ctxRec: the parts' tokens
without their items; the whole prompt only on the routes that can be a
session's latest), keyed by the size and mtime of the day's files. A
day is read again only when its files change. The answer is given again
while its days and the trace are unchanged, for at most a minute.
- /api/tune keeps each file's calls as tune.Call (~100 B each), keyed by
size, mtime and agent, read one file at a time through
sessions.ReadCallSource. A message copied into a resumed session is
still counted once (the earlier file owns it, as in sessions.Calls). The
advice stands for a minute, and apply or undo lets it go at once.
- tune's stable sorts use slices.SortStableFunc instead of
sort.SliceStable, which was most of the CPU over 300k calls.
Measured on mote with a generated fixture: 8 days of 20,000 routes each
(7 gzipped, built from a real 2.2 KB route), and 200 Claude Code files of
1,500 calls each (300k calls). The scratch bench was not committed. mote
was at load 130-170 from other suites, so wall times are noisy; CPU time
is from rusage and peak heap is sampled every 2 ms.
/api/context before: 1st ask 4.2s CPU, 158 MB peak heap, 202 MB RSS;
every refresh 0.63s CPU, 111-121 MB peak heap
after: 1st ask 0.84s CPU, 48-55 MB, 90-93 MB RSS;
every refresh 3-5 ms CPU, 21-34 MB
/api/tune before: 1st ask 4.3s CPU, 498 MB peak heap, 717 MB RSS;
every refresh 1.0s CPU, 370-494 MB peak heap
after: 1st ask 0.83s CPU, 373 MB, 409 MB RSS;
within a minute 0 CPU, nothing read;
after a minute with a file changed, that file is
read and the advice worked out again (~0.5s CPU)
Tests:
- TestContextReadsADayOnce: a day whose files are unchanged is not read
again; a grown day is, and only that day; each session's latest prompt
keeps its items; the kept answer equals contextOf over every route.
- TestTuneReadsAFileOnce: an unchanged file is not read again; a grown
one is; a copied message counts once; the call count matches
tuneOf(sessions.Calls(...)); within a minute nothing is read.
- TestHistoryParsesOnlyAGzDaysLatest: a 5,200-route gz day parses at
most 2,200 routes.
Broken by hand: always re-read the day, skip tune's file check, drop
History's count-first block. All three tests went red (days read 4
times, files read 5 times, 5,200 routes parsed), then green again once
restored.
Run on mote at this tree:
- gofmt -l: clean.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows
go vet ./..., go build ./...: all ok.
- go test ./internal/gui ./internal/gateway ./internal/tune
./internal/sessions: tune and sessions ok. gui is red only on
TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27 on mote). gateway
is red only on the Antigravity turned-away tests and
TestCodexSpentGrokFailingIsTold, which are red on main (#4362). This
change touches neither.
- GUI (node --test, Chromium and WebKit): context-*.test.cjs,
compact-one-value, usage-refresh, usage-refresh-flight and
usage-stale-answer: 270 of 273 pass. The 3 reds are context-map's
"two tiles overlap" in WebKit at 420px (en, ja, de), the overlap
#3488 has under load (mote at load ~160). No .js changed, so app.js's budget is
untouched.
Not tried:
- A real 1-core, 1 GB box.
- Codex rollouts in the fixture: Codex files take the same path.
- internal/usage's codexCalls, which still uses sessions.AgentCalls
(Codex only, on the Usage identity path) and is unchanged.
- tune's first ask, and each ask after a minute with a file changed,
still works over the range's calls at once: ~30 MB kept per 300k calls,
and a few hundred MB peak while the advice is worked out. It is bounded
by the calls in the range, not by a fixed size.
499 lines
15 KiB
Go
499 lines
15 KiB
Go
package gateway
|
|
|
|
// The routing history: every request's route, once it is done, kept on
|
|
// disk so the Routing view can show and replay a day long gone — the trace
|
|
// holds only the last few, and only since the gateway started. A day is a
|
|
// file of its own, one route a line; a day that is over is gzipped, the
|
|
// routes being much alike. Kept historyDays days and historyBytes in all,
|
|
// the oldest days going first.
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/steady"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
const (
|
|
historyDays = 30
|
|
historyBytes = 200 << 20
|
|
historyMax = 2000 // the routes of one day served at most: its latest
|
|
)
|
|
|
|
// HistoryDir is where the days are kept: ~/.config/magpie/routing.
|
|
func HistoryDir() string { return filepath.Join(filepath.Dir(provider.Path()), "routing") }
|
|
|
|
// keepRoutes is whether done routes are written to disk. Off in the
|
|
// package's tests: a write still going when a test ends would put the
|
|
// routing directory back into its temporary HOME as that is removed.
|
|
var keepRoutes = true
|
|
|
|
// testSandbox is testenv.Marker, which gateway can't import outside its
|
|
// tests: set in a test binary testenv isolated.
|
|
const testSandbox = "MAGPIE_TEST_SANDBOX"
|
|
|
|
// keepsRoutes says whether a done route is written to disk: never in a test
|
|
// binary testenv isolated. The write runs behind the request that ended the
|
|
// route, so one still going when its test ended put routing/ back into the
|
|
// test's config folder as that was removed, failing its TempDir cleanup
|
|
// with "directory not empty": internal/gui's TestContextTextAPI (#1543),
|
|
// and any package whose tests send a request through a gateway (gui, tui,
|
|
// agent, davsync). No test outside this package reads the history back.
|
|
func keepsRoutes() bool { return keepRoutes && os.Getenv(testSandbox) == "" }
|
|
|
|
var history struct {
|
|
mu sync.Mutex
|
|
pruned time.Time
|
|
counts map[string]counted // a day's file → its requests, as of its size
|
|
}
|
|
|
|
type counted struct {
|
|
size int64
|
|
n 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"
|
|
|
|
// historyClock is the time saveRoute prunes the days by: which day is today,
|
|
// and which are over or too old. A variable for the tests.
|
|
var historyClock = time.Now
|
|
|
|
// saveRoute adds a done route to its day. Errors are swallowed: keeping
|
|
// the history must never break a call.
|
|
func saveRoute(r Route) {
|
|
r.Seq = 0
|
|
b, err := json.Marshal(r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
history.mu.Lock()
|
|
defer history.mu.Unlock()
|
|
dir := HistoryDir()
|
|
if os.MkdirAll(dir, 0o755) != nil {
|
|
return
|
|
}
|
|
f, err := os.OpenFile(filepath.Join(dir, r.Time.Local().Format(dayForm)+".jsonl"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
return
|
|
}
|
|
f.Write(append(b, '\n'))
|
|
f.Close()
|
|
if now := historyClock(); now.Sub(history.pruned) > time.Hour {
|
|
history.pruned = now
|
|
pruneHistory(dir, now)
|
|
}
|
|
}
|
|
|
|
// pruneHistory gzips the days that are over, and drops the days older than
|
|
// historyDays, then the oldest until what is left fits historyBytes. A day
|
|
// goes whole, as it does once its late routes are in its .gz: a late route's
|
|
// file left beside the .gz (gzipFile) goes with it. A file that can't be
|
|
// removed stays.
|
|
func pruneHistory(dir string, now time.Time) {
|
|
// a .gz.tmp left by a magpie stopped before renaming it (gzipFile) goes
|
|
// once it is an hour old: a younger one may be another magpie's, still
|
|
// being written
|
|
es, _ := os.ReadDir(dir)
|
|
for _, e := range es {
|
|
if !strings.HasSuffix(e.Name(), ".jsonl.gz.tmp") {
|
|
continue
|
|
}
|
|
if fi, err := e.Info(); err == nil && now.Sub(fi.ModTime()) > time.Hour {
|
|
os.Remove(filepath.Join(dir, e.Name()))
|
|
}
|
|
}
|
|
today := now.Local().Format(dayForm)
|
|
for _, d := range historyFiles(dir) {
|
|
if d.day < today && !strings.HasSuffix(d.path, ".gz") {
|
|
gzipFile(d.path)
|
|
}
|
|
}
|
|
oldest := now.Local().AddDate(0, 0, -historyDays+1).Format(dayForm)
|
|
days := historyFiles(dir)
|
|
var total int64
|
|
for _, d := range days {
|
|
total += d.size
|
|
}
|
|
drop := "" // the day being dropped, whole: historyFiles puts its files next to each other
|
|
for _, d := range days { // oldest first
|
|
if d.day != drop && (d.day >= oldest && total <= historyBytes || d.day == today) {
|
|
continue
|
|
}
|
|
drop = d.day
|
|
if os.Remove(d.path) == nil {
|
|
total -= d.size
|
|
}
|
|
}
|
|
}
|
|
|
|
// gzipFile gzips a day's file into its .gz. A day gzipped already keeps its
|
|
// .gz, the file's routes going after its own as a gzip member of their own
|
|
// (read back as one stream): a route that began before midnight and was done
|
|
// after the first prune past it is written to its day's file again. A route
|
|
// in the .gz already, from a file added and left behind (magpie stopped
|
|
// before removing it), isn't added again. A .gz cut short, or ending in a
|
|
// route cut short (an older magpie stopped, or the disk filled, as it or its
|
|
// file was written), is written anew from its whole routes and the file's
|
|
// when the file has a route to add: after the break the file's routes
|
|
// couldn't be read, and after a route cut short the first of them would join
|
|
// it. A last route whole but for its newline is kept. The .gz is read a route
|
|
// at a time, never held whole unzipped; it is replaced whole, never seen half
|
|
// written, and the file goes once its routes are in. A .gz that can't be read
|
|
// from disk is left as it is, and the file with it, for a later prune.
|
|
func gzipFile(path string) {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
old, err := os.ReadFile(path + ".gz")
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return
|
|
}
|
|
// the file's routes, each true once the .gz is seen to have it: a set
|
|
// built from the file, a few routes, not from the whole day
|
|
in := map[string]bool{}
|
|
whole := true // the .gz, if there is one, can have routes go after it
|
|
if len(old) > 0 {
|
|
for l := range bytes.Lines(b) {
|
|
in[string(bytes.TrimSpace(l))] = false
|
|
}
|
|
whole = gzRoutes(old, func(l []byte) {
|
|
if _, ok := in[string(l)]; ok {
|
|
in[string(l)] = true
|
|
}
|
|
})
|
|
}
|
|
buf := new(bytes.Buffer)
|
|
if whole {
|
|
buf = bytes.NewBuffer(old)
|
|
}
|
|
z := gzip.NewWriter(buf)
|
|
nl := []byte{'\n'}
|
|
add := func(l []byte) {
|
|
z.Write(l)
|
|
z.Write(nl)
|
|
}
|
|
if !whole {
|
|
gzRoutes(old, add) // written anew, its whole routes first
|
|
}
|
|
added := false
|
|
for l := range bytes.Lines(b) {
|
|
if l = bytes.TrimSpace(l); len(l) > 0 && !in[string(l)] {
|
|
add(l)
|
|
added = true
|
|
}
|
|
}
|
|
if added {
|
|
tmp := path + ".gz.tmp"
|
|
if z.Close() != nil || os.WriteFile(tmp, buf.Bytes(), 0o600) != nil || steady.Rename(tmp, path+".gz") != nil {
|
|
os.Remove(tmp)
|
|
return
|
|
}
|
|
}
|
|
os.Remove(path)
|
|
}
|
|
|
|
// gzRoutes calls f with each route of a day's .gz in turn, a last route
|
|
// whole but for its newline among them and one cut short left out. It says
|
|
// whether the .gz read to its end, its last route ending in a newline: what
|
|
// a gzip member added after it needs to be read.
|
|
func gzRoutes(gz []byte, f func([]byte)) bool {
|
|
z, err := gzip.NewReader(bytes.NewReader(gz))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
r := bufio.NewReader(z)
|
|
for {
|
|
line, err := r.ReadBytes('\n')
|
|
l := bytes.TrimSpace(line)
|
|
if err != nil {
|
|
if len(l) > 0 && json.Valid(l) {
|
|
f(l)
|
|
}
|
|
return err == io.EOF && len(l) == 0
|
|
}
|
|
if len(l) > 0 {
|
|
f(l)
|
|
}
|
|
}
|
|
}
|
|
|
|
type dayFile struct {
|
|
day, path string
|
|
size int64
|
|
}
|
|
|
|
// historyFiles are the kept days, oldest first.
|
|
func historyFiles(dir string) []dayFile {
|
|
es, _ := os.ReadDir(dir)
|
|
var out []dayFile
|
|
for _, e := range es {
|
|
name := e.Name()
|
|
day, _, _ := strings.Cut(name, ".")
|
|
if _, err := time.Parse(dayForm, day); err != nil || !(strings.HasSuffix(name, ".jsonl") || strings.HasSuffix(name, ".jsonl.gz")) {
|
|
continue
|
|
}
|
|
info, err := e.Info()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
out = append(out, dayFile{day, filepath.Join(dir, name), info.Size()})
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].day < out[j].day })
|
|
return out
|
|
}
|
|
|
|
// HistoryDay is a kept day, and how many requests it had.
|
|
type HistoryDay struct {
|
|
Day string `json:"day"`
|
|
Requests int `json:"requests"`
|
|
}
|
|
|
|
// History is the days kept, newest first, and the routes of day, oldest
|
|
// first — its latest historyMax, and whether there were more.
|
|
func History(day string) (days []HistoryDay, routes []Route, cut bool) {
|
|
history.mu.Lock()
|
|
defer history.mu.Unlock()
|
|
routes = []Route{}
|
|
if history.counts == nil {
|
|
history.counts = map[string]counted{}
|
|
}
|
|
total := 0
|
|
for _, d := range historyFiles(HistoryDir()) {
|
|
// 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; a .jsonl only past what was tallied
|
|
c, ok := history.counts[d.path]
|
|
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]
|
|
}
|
|
readDayFrom(d.path, from, func(line []byte) bool {
|
|
routes = appendRoute(routes, line)
|
|
return true
|
|
})
|
|
}
|
|
} else if d.day == day || !ok || c.size != d.size {
|
|
if d.day == day && (!ok || c.size != d.size) {
|
|
// counted first, routes left as lines: a busy day's every
|
|
// route parsed at once, to keep its latest historyMax, was
|
|
// hundreds of MB, and a small server running the Context
|
|
// tab went down (Yayoi_no_yume on X)
|
|
n := 0
|
|
readDay(d.path, func([]byte) bool { n++; return true })
|
|
c, ok = counted{size: d.size, n: n}, true
|
|
}
|
|
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{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 {
|
|
days[0].Requests += n // a late route's file beside the day's .gz
|
|
} else {
|
|
days = append([]HistoryDay{{d.day, n}}, days...)
|
|
}
|
|
}
|
|
}
|
|
sort.SliceStable(routes, func(i, j int) bool { return routes[i].Time.Before(routes[j].Time) })
|
|
if len(routes) > historyMax {
|
|
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
|
|
|
|
// historyParsed counts the routes History has parsed, for the tests.
|
|
var historyParsed atomic.Int64
|
|
|
|
func appendRoute(routes []Route, line []byte) []Route {
|
|
historyParsed.Add(1)
|
|
var r Route
|
|
if json.Unmarshal(line, &r) != nil {
|
|
return routes
|
|
}
|
|
r.routedAgain()
|
|
r.Prompt.skillsApart()
|
|
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
|
|
}
|
|
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
|
|
// another magpie's routing group was marked swapped for the member it
|
|
// routed to, and is routed (usage.GroupRouted). A try kept marked swapped
|
|
// for its model spelled with other separators (deepseek-v4.1-flash
|
|
// answered as deepseek-v4-1-flash) wasn't swapped (usage.SameSpelled), nor
|
|
// was one answered under Google's name for the deployment serving its
|
|
// Gemini model (gemini-3.8-flash as gemini-3.8-flash-n) or xAI's for a Grok
|
|
// one (grok-4.7 as grok-4.7-build), usage.ServingName, nor one answered
|
|
// under the name its provider's list gives it (usage.ListedAs).
|
|
func (r *Route) routedAgain() {
|
|
for i := range r.Tries {
|
|
if tr := &r.Tries[i]; tr.Swapped && usage.GroupRouted(tr.Model, tr.Served) {
|
|
tr.Swapped, tr.Routed = false, true
|
|
} else if tr.Swapped && (usage.SameSpelled(tr.Model, tr.Served) || usage.ServingName(tr.Model, tr.Served) || usage.ListedAs(tr.ID, tr.Model, tr.Served)) {
|
|
tr.Swapped = false
|
|
}
|
|
}
|
|
if n := len(r.Tries); n > 0 && r.Swapped && !r.Tries[n-1].Swapped && !r.Tries[n-1].Routed && r.Served == r.Tries[n-1].Served {
|
|
r.Swapped = false
|
|
}
|
|
if n := len(r.Tries); n > 0 && r.Swapped && r.Tries[n-1].Routed && r.Served == r.Tries[n-1].Served {
|
|
r.Swapped, r.Routed = false, true
|
|
}
|
|
}
|
|
|
|
// readDay calls f with each line of a day's file.
|
|
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 = file
|
|
if strings.HasSuffix(path, ".gz") {
|
|
z, err := gzip.NewReader(file)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer z.Close()
|
|
r = z
|
|
}
|
|
sc := bufio.NewScanner(r)
|
|
sc.Buffer(make([]byte, 64<<10), 16<<20)
|
|
for sc.Scan() {
|
|
if line := bytes.TrimSpace(sc.Bytes()); len(line) > 0 {
|
|
if !f(line) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// HistoryRoute searches the request's day and its neighbours (the browser and
|
|
// gateway may use different time zones), without holding up history writes.
|
|
func HistoryRoute(id int64, day time.Time) (Route, bool) {
|
|
needle := []byte(`"id":` + strconv.FormatInt(id, 10) + `,`)
|
|
for _, offset := range []int{0, -1, 1} {
|
|
name := filepath.Join(HistoryDir(), day.AddDate(0, 0, offset).Format(dayForm)+".jsonl")
|
|
for _, path := range []string{name, name + ".gz"} {
|
|
var found Route
|
|
readDay(path, func(line []byte) bool {
|
|
if !bytes.Contains(line, needle) {
|
|
return true
|
|
}
|
|
if json.Unmarshal(line, &found) != nil || found.ID != id {
|
|
found = Route{}
|
|
return true
|
|
}
|
|
found.routedAgain()
|
|
found.Prompt.skillsApart()
|
|
return false
|
|
})
|
|
if found.ID != 0 {
|
|
return found, true
|
|
}
|
|
}
|
|
}
|
|
return Route{}, false
|
|
}
|