usage: a request state hashes Codex's thread identities without encoding them
readRequestState json.Marshal()ed every Codex thread's identity (~20k over a real history) into a new buffer only to write it into the key's hash, on every requests page and effort ask: every 15 s poll. They are now written into the hash directly, each field length-prefixed, in the order of their keys. The key lives in memory only (the disk index is keyed by meta), so the changed form costs nothing on upgrade. Benchmark, 20k identities (a throwaway benchmark, not committed): json.Marshal + hash 7.06 ms, 6.49 MB, 40012 allocs an ask; now 3.36 ms, 0.36 MB, 3 allocs. In a 12-poll real-history run json.Marshal under readRequestState was 116 MB. Test: TestCodexIdentitiesHashed (same identities in another order hash the same; a thread turning subagent changes it; ab|c is not a|bc; none is not one). It fails with the length prefixes taken out by hand. The rest of the package's tests passed with this hash removed altogether: an identity changes only with a rollout, whose size and time are in the key too. Run at this tree: go test ./internal/usage/ ok (8 runs; one earlier run of this change printed a bare FAIL whose test I lost to a tail, not seen again in 8 package runs and 30 of the effort tests); gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage ok.
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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() }
|
||||
|
||||
Reference in New Issue
Block a user