Files
magpie/internal/sessions/call_revisions.go
T
yetone c14fa0ce93 Bring back 919780414 and 88d2962e0, which 698ed4adb undid
698ed4adb (a busy port names its holder) was committed from a tree made
before 919780414 and 88d2962e0 landed (09:38, eight minutes before it),
and took both out with no word in its body: quotatable.js, the Usage
Cards / Table switch and its test, and sessions' 16-byte head and
prefix hashes (digest.go) were gone from main from v0.1.3823. This puts
both back as they were, on top of 698ed4adb's own change, which is kept.

Run at this tree, on mote (cherry-pick of both onto origin/main
db0c1b039, no conflicts):
- gofmt -l internal/sessions: clean.
- go vet ./internal/sessions ./internal/gui; GOOS=windows and
  GOOS=linux -tags nogui go vet ./internal/sessions: ok.
- go test -count=1 ./internal/sessions: ok 83.6s.
- go test -run 'Budget|Lazy|I18n' ./internal/gui: ok (the core script
  budget holds with both changes).
- GUI, chromium and webkit: usage-quota-table, gateway-port-holder,
  i18n-keys, i18n-en-no-cjk, old-webkit: 44 of 46. The two reds are
  old-webkit's "compat.js is the first" (headerpresets.js, #6030) and
  "the styles hold up in Safari 15.0" (#5919), red the same on an
  origin/main db0c1b039 copy.
Not run: the full Go and GUI suites.
2026-11-12 09:57:52 +08:00

75 lines
2.2 KiB
Go

package sessions
import (
"bytes"
"encoding/gob"
"os"
"path/filepath"
"strings"
)
// A Claude Code session's revision window (claudeUsageState) lived in memory
// alone, so once magpie started again, or let a ninth live session's window
// go, every Claude file that had grown was read again from its first line: a
// live 2 GB session took seconds of the first Requests page after every
// update (#3493). The window is bounded (claudeRevisionEntries), so it is
// kept beside the file's shard, with where the read that made it stopped.
// callRevisions is a shard's Claude window, for the read that ended at Off.
type callRevisions struct {
Off int64
ContentHash string
CompactMs int64
Window []claudeWindowEntry
}
func callRevisionsPath(path string) string {
return strings.TrimSuffix(callCachePath(path), ".gob") + ".rev"
}
// writeCallRevisions keeps st's window beside its shard, or takes away a
// window kept before when st has none.
func writeCallRevisions(st *callFile) {
path := callRevisionsPath(st.Path)
if st.Claude == nil {
_ = os.Remove(path)
return
}
r := callRevisions{Off: st.Off, ContentHash: st.ContentHash.String(), CompactMs: st.CompactMs, Window: st.Claude.window()}
var data bytes.Buffer
if gob.NewEncoder(&data).Encode(r) != nil {
return
}
out, err := os.CreateTemp(filepath.Dir(path), "calls-*.tmp")
if err != nil {
return
}
defer os.Remove(out.Name())
_, err = out.Write(data.Bytes())
if closeErr := out.Close(); err == nil {
err = closeErr
}
if err == nil {
_ = os.Rename(out.Name(), path)
}
}
// withCallRevisions is old with the window kept for it, when old is a
// Claude file's read without one and the window kept is of that same read.
func withCallRevisions(old *callFile) *callFile {
if old == nil || old.Claude != nil || (old.Agent != "claude" && old.Agent != "claude-desktop") || old.ContentHash == (digest{}) {
return old
}
data, err := os.ReadFile(callRevisionsPath(old.Path))
if err != nil {
return old
}
var r callRevisions
if gob.NewDecoder(bytes.NewReader(data)).Decode(&r) != nil || r.Off != old.Off || parseDigest(r.ContentHash) != old.ContentHash {
return old
}
c := *old
c.Claude, c.CompactMs = claudeWindowOf(r.Window), r.CompactMs
return &c
}