Skip to content

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

usage: a request state hashes Codex's thread identities without encoding them

yetonec2c08850f695
diff --git a/internal/usage/codex_identity_hash_test.go b/internal/usage/codex_identity_hash_test.go
new file mode 100644
index 000000000..da7528849
--- /dev/null
+++ b/internal/usage/codex_identity_hash_test.go
@@ -0,0 +1,46 @@
+package usage
+
+import (
+ "crypto/sha256"
+ "fmt"
+ "testing"
+
+ "github.com/yetone/magpie/internal/sessions"
+)
+
+// A request state's key changes with any thread's identity, and only with
+// one: the same identities give the same key however the map was filled,
+// and a field's end is not read as the next one's start.
+func TestCodexIdentitiesHashed(t *testing.T) {
+ sum := func(ids map[string]sessions.CodexIdentity) string {
+ h := sha256.New()
+ hashCodexIdentities(h, ids)
+ return fmt.Sprintf("%x", h.Sum(nil))
+ }
+ many := func(n int, from int) map[string]sessions.CodexIdentity {
+ ids := map[string]sessions.CodexIdentity{}
+ for i := range n { // more than one write's worth
+ k := fmt.Sprintf("019a%028d", (i+from)%n)
+ ids[k] = sessions.CodexIdentity{ID: k, Role: "main", RootSession: k}
+ }
+ return ids
+ }
+ base := sum(many(5000, 0))
+ if got := sum(many(5000, 1234)); got != base {
+ t.Fatal("the same identities, filled in another order, hashed apart")
+ }
+ child := many(5000, 0)
+ k := fmt.Sprintf("019a%028d", 4321)
+ child[k] = sessions.CodexIdentity{ID: k, Role: "subagent", ParentSession: "p", RootSession: "p"}
+ if sum(child) == base {
+ t.Fatal("a thread that became a subagent left the key as it was")
+ }
+ a := map[string]sessions.CodexIdentity{"t": {ID: "ab", Role: "c"}}
+ b := map[string]sessions.CodexIdentity{"t": {ID: "a", Role: "bc"}}
+ if sum(a) == sum(b) {
+ t.Fatal("fields run together: ab|c hashed as a|bc")
+ }
+ if sum(nil) == sum(a) {
+ t.Fatal("no identities hashed as one")
+ }
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index c759ad2cd..34dd9e72a 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -7,8 +7,10 @@ package usage
import (
"crypto/sha256"
+ "encoding/binary"
"encoding/json"
"fmt"
+ "hash"
"hash/maphash"
"io"
"maps"
@@ -640,7 +642,6 @@ func readRequestState(now time.Time) requestState {
return strings.Compare(a.Path, b.Path)
})
ids, _ := json.Marshal(provider.SessionIdentities(sessions.CodexDir()))
- codexIdentityKey, _ := json.Marshal(codexIdentities)
renamed := provider.UsageRenames().Key()
cfg := settings.Load()
prices, _ := json.Marshal(cfg.ModelPrices)
@@ -663,7 +664,7 @@ func readRequestState(now time.Time) requestState {
h.Reset()
snapshot := readLogSnapshot()
fmt.Fprint(h, meta, snapshot.version, now.In(time.Local).Format("2006-01-02 MST"))
- h.Write(codexIdentityKey)
+ hashCodexIdentities(h, codexIdentities)
for _, s := range sources {
fmt.Fprintf(h, "%s:%d:%d;", s.Path, s.Size, s.Modified.UnixNano())
}
@@ -675,6 +676,30 @@ func readRequestState(now time.Time) requestState {
root: requestRoot(), meta: meta, key: fmt.Sprintf("%x", h.Sum(nil))}
}
+// hashCodexIdentities writes ids into h in the order of their keys. They
+// were json.Marshal()ed first, ~20k threads' identities encoded into a new
+// buffer of several MB on every page and effort ask.
+func hashCodexIdentities(h hash.Hash, ids map[string]sessions.CodexIdentity) {
+ keys := make([]string, 0, len(ids))
+ for k := range ids {
+ keys = append(keys, k)
+ }
+ slices.Sort(keys)
+ b := make([]byte, 0, 32<<10)
+ for _, k := range keys {
+ id := ids[k]
+ for _, s := range [...]string{k, id.ID, id.Role, id.ParentSession, id.RootSession} {
+ b = binary.AppendUvarint(b, uint64(len(s)))
+ b = append(b, s...)
+ }
+ if len(b) > 30<<10 {
+ h.Write(b)
+ b = b[:0]
+ }
+ }
+ h.Write(b)
+}
+
// requestRoot is the logs a request state is read from: another home's
// answer is no answer.
func requestRoot() string { return Path() + "|" + catalog.CachePath() }