Skip to content

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

usage: requests-v1.bin is of the code that makes rows, not of the binary

yetone42ab935516b6
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go
index 71f6f44eb..2f37fd8b9 100644
--- a/internal/sessions/call_cache.go
+++ b/internal/sessions/call_cache.go
@@ -44,6 +44,10 @@ import (
// the version is part of the directory's name, so a cache written by an older
// one is never read (pruneCalls).
const callCacheVersion = "calls-v15"
+
+// CallCacheVersion is callCacheVersion, for a cache of what is made of the
+// calls (usage's requests-v1.bin).
+const CallCacheVersion = callCacheVersion
const maxKeptCalls = 131072
const maxKeptFiles = 64
diff --git a/internal/usage/request_disk.go b/internal/usage/request_disk.go
index 04f896ec7..71c7d01f5 100644
--- a/internal/usage/request_disk.go
+++ b/internal/usage/request_disk.go
@@ -2,12 +2,18 @@ package usage
import (
"bufio"
+ "crypto/sha256"
+ "embed"
"encoding/binary"
"errors"
+ "fmt"
"hash/crc32"
"io"
+ "io/fs"
"os"
"path/filepath"
+ "strings"
+ "sync"
"time"
"github.com/yetone/magpie/internal/sessions"
@@ -19,7 +25,7 @@ import (
// could show, and so did a page asked for once two idle minutes had trimmed
// the chunks (#3493). An entry stands for its session file while the file's
// size and mtime are the same, as a call shard does, and the whole file
-// while the binary that wrote it and what prices and names a call (the
+// while the code that wrote it and what prices and names a call (the
// index's root and meta) are the same.
//
// The file is a header, then entries one after another, each its length,
@@ -32,15 +38,44 @@ func requestDiskPath() string {
return filepath.Join(filepath.Dir(sessions.CachePath()), "requests-v1.bin")
}
-// requestDiskBinary names the binary a file was written by: a new build may
-// make a row of a call differently.
-var requestDiskBinary = func() string {
- exe, err := os.Executable()
- if err != nil {
- return ""
+// requestDiskBinary names the code a file was written by: another may make
+// a row of a call differently. It was the binary itself, so every upgrade
+// read every session file again (~15.6k call shards, a cold start's
+// longest step); it is the call shards' version and this package's own
+// source, which 17 of 99 releases changed.
+var requestDiskBinary = sync.OnceValue(func() string {
+ h := sha256.New()
+ io.WriteString(h, sessions.CallCacheVersion)
+ // unread, it is the binary's, as it was
+ binary := func() string {
+ exe, err := os.Executable()
+ if err != nil {
+ return ""
+ }
+ return statKey(exe)
}
- return statKey(exe)
-}
+ names, err := fs.Glob(ownSource, "*.go")
+ if err != nil || len(names) == 0 {
+ return binary()
+ }
+ for _, name := range names { // in lexical order
+ if strings.HasSuffix(name, "_test.go") {
+ continue
+ }
+ b, err := ownSource.ReadFile(name)
+ if err != nil {
+ return binary()
+ }
+ fmt.Fprintf(h, "\x00%s\x00%d\x00", name, len(b))
+ h.Write(b)
+ }
+ return fmt.Sprintf("source %x", h.Sum(nil))
+})
+
+// ownSource is this package's source, for requestDiskBinary.
+//
+//go:embed *.go
+var ownSource embed.FS
// requestDiskLeast is how many session files must want reading before the
// file is read for them: a page after a call reads one or two again.
diff --git a/internal/usage/request_disk_test.go b/internal/usage/request_disk_test.go
index c11017729..3f0585e18 100644
--- a/internal/usage/request_disk_test.go
+++ b/internal/usage/request_disk_test.go
@@ -1,9 +1,12 @@
package usage
import (
+ "crypto/sha256"
"fmt"
+ "io"
"os"
"path/filepath"
+ "strings"
"sync/atomic"
"testing"
"time"
@@ -111,3 +114,32 @@ func TestRequestChunksFromDisk(t *testing.T) {
t.Fatalf("a bad file: %d reads, want %d", reads.Load(), all)
}
}
+
+// The file is of the code that makes a call's rows, not of the binary: an
+// upgrade that changed no source of this package reads it as the last run
+// left it. The name is of every source file here but the tests, and of
+// the call shards' version.
+func TestRequestDiskNamesItsSource(t *testing.T) {
+ names, err := filepath.Glob("*.go")
+ if err != nil {
+ t.Fatal(err)
+ }
+ h := sha256.New()
+ io.WriteString(h, sessions.CallCacheVersion)
+ sources := 0
+ for _, name := range names {
+ if strings.HasSuffix(name, "_test.go") {
+ continue
+ }
+ b, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ fmt.Fprintf(h, "\x00%s\x00%d\x00", name, len(b))
+ h.Write(b)
+ sources++
+ }
+ if want := fmt.Sprintf("source %x", h.Sum(nil)); requestDiskBinary() != want || sources < 10 {
+ t.Fatalf("named %q, want %q of %d source files", requestDiskBinary(), want, sources)
+ }
+}