history: today's plain file takes no room from the days before it

pruneHistory held every file in routing/ to historyBytes (200 MB),
today's plain .jsonl included. Today is never dropped, and its file is
written plain, about ten times what it is once gzipped (this Mac's
2026-11-06: 5,785 routes, 1.1 MB gzipped). On a busy gateway, today's
file took the room of the days before it, and every prune (the first
save after a start, then hourly) dropped the oldest days until it fit;
past 200 MB plain, it dropped every day but today. The Context tab then
listed only today's sessions, while Usage's Requests, read from the
ledger, kept them. The budget now holds the days that are over; today's
file is counted from the next day on, gzipped.

From linkaww's report: on their Docker magpie, after each nightly
update, Context lists only today's sessions while Requests keeps the
older ones. NOT confirmed as their cause. A real Docker run on
xiaomi-gpu (the image built from this tree, a named volume on /config,
a fake upstream in a second container) kept every session across
`docker restart` and across rm + run on the same volume, as the
documented update does; so did a sandbox chain (desktop magpie ->
remote magpie with a gateway key, the remote restarted). A plain
restart loses nothing; this budget is the one path found that drops
past days while the ledger keeps them. Their routing/ listing would
tell.

History already dropped is gone: the routing days are their only copy,
and the ledger keeps no prompt tokens or sessions to rebuild them from.

Run (mote, at 4461957b9 + this):
- TestHistoryTodayTakesNoRoomFromPastDays, new: red on the old
  history.go (kept [2026-09-30], want the three days before it too),
  green with the change.
- go test -run 'History|Route|Prune' ./internal/gateway: ok.
- go test -run Context ./internal/gui: ok.
- gofmt -l; GOOS=linux go vet -tags nogui and GOOS=windows go vet
  ./internal/gateway: clean.
Not tried: linkaww's own container and data; the full suite (run after
the tag, per the loop rules). Not changed: the remote-magpie hop passes
no x-claude-code-agent-id on, so a Claude Code subagent's calls through
it count as the main session's on the remote's Context tab; a separate
change.

Reported-by: linkaww on Discord
This commit is contained in:
yetone
2026-11-07 17:30:06 +08:00
parent 4461957b9e
commit f13d5f16f4
3 changed files with 47 additions and 8 deletions
File diff suppressed because one or more lines are too long
+14 -7
View File
@@ -4,8 +4,8 @@ package gateway
// 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.
// routes being much alike. Kept historyDays days, and historyBytes for the
// days that are over, the oldest days going first.
import (
"bufio"
@@ -113,10 +113,10 @@ func saveRoute(r Route) {
}
// 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.
// historyDays, then the oldest until the days that are over fit
// 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
@@ -138,9 +138,16 @@ func pruneHistory(dir string, now time.Time) {
}
oldest := now.Local().AddDate(0, 0, -historyDays+1).Format(dayForm)
days := historyFiles(dir)
// the days that are over, held to historyBytes: today's file isn't
// counted, being kept whatever its size and written plain, at many
// times the size it is gzipped at once it is over. Counted, a busy
// day's plain file took the room of the days before it, and the
// Context tab showed only today's sessions (linkaww on Discord)
var total int64
for _, d := range days {
total += d.size
if d.day != today {
total += d.size
}
}
drop := "" // the day being dropped, whole: historyFiles puts its files next to each other
for _, d := range days { // oldest first
+32
View File
@@ -87,6 +87,38 @@ func TestHistoryKeepsDaysAndDropsOld(t *testing.T) {
}
}
// Today's plain file, however big, takes no room from the days before it:
// it is kept whatever its size, and is a fraction of it once gzipped. A busy
// day counted plain dropped every day before it, and the Context tab
// listed only today's sessions (linkaww on Discord).
func TestHistoryTodayTakesNoRoomFromPastDays(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
now := historyNoon(t)
dir := HistoryDir()
saveRoute(Route{ID: 1, Time: now, Model: "a", Done: true})
var want []string
for i := 3; i >= 1; i-- {
day := now.AddDate(0, 0, -i).Format(dayForm)
if err := os.WriteFile(filepath.Join(dir, day+".jsonl.gz"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
want = append(want, day)
}
want = append(want, now.Format(dayForm))
// today's file past the whole budget (sparse: no bytes written)
if err := os.Truncate(filepath.Join(dir, now.Format(dayForm)+".jsonl"), historyBytes+1); err != nil {
t.Fatal(err)
}
pruneHistory(dir, now)
var left []string
for _, d := range historyFiles(dir) {
left = append(left, d.day)
}
if !slices.Equal(left, want) {
t.Fatalf("kept %v, want %v: the days before today dropped for today's size", left, want)
}
}
// A route that began on a day gzipped already, its reply streamed past
// midnight and past that hour's prune, is kept with the day's other
// routes: its file is added to the day's .gz, not written over it, and the