sessions: the kept stats and listing hold an untitled id once, not a map entry
keptStats and keptList noted every answer Codex's titles gave in a map[id]string, to ask each again before the kept ranges are given out. Nearly every id has no title, and the map held 3.12MB on an 18k-file history. titleAnswers keeps the named ones in a map and the untitled ids in a slice, sorted, without repeats or slack once settled; still() asks each again as before. Both caches use it. Run (mote, real-account copy, idle 180s, network blocked): - statsKept.keptTitle.func4 3.12MB -> under the profile's cut; total inuse 107.40MB -> 104.23MB. - TestStatsKeptBetweenPolls now also names a Codex thread after the ranges were kept and checks the title reaches them. TestTitleAnswersSettled checks repeats and slack go. - Broken by hand (still() skipping untitled ids, settle not compacting): TestListKeptBetweenPolls, TestStatsKeptBetweenPolls and TestTitleAnswersSettled red; green on this code. - go test ./internal/sessions/ ok; go vet darwin, linux, windows ok; gofmt clean. Not tried: the full suite (only internal/sessions reads these caches).
This commit is contained in:
@@ -21,7 +21,7 @@ var keptList struct {
|
||||
// the answers the listing got from the prices, Codex's names and
|
||||
// Claude Desktop's, asked again before it is given out
|
||||
prices map[string]*catalog.Price
|
||||
titles map[string]string
|
||||
titles titleAnswers
|
||||
desktop map[string]string
|
||||
// the command an agy row resumes with (AgyStart), when one is listed:
|
||||
// it changes when agy is wired to magpie's gateway or let go of
|
||||
@@ -43,7 +43,7 @@ var listKeptFor = time.Minute
|
||||
func keptListing(files []file, limit int, price func(string) *catalog.Price, titles func(string) string) ([]Session, bool) {
|
||||
k := &keptList
|
||||
if k.out == nil || k.gen != cacheGeneration || k.limit != limit || time.Since(k.at) >= listKeptFor ||
|
||||
!keptStill(k.files, files, k.prices, k.titles, k.desktop, price, titles) || k.agy && agyStart() != k.agyStart {
|
||||
!keptStill(k.files, files, k.prices, &k.titles, k.desktop, price, titles) || k.agy && agyStart() != k.agyStart {
|
||||
forgetKeptList()
|
||||
return nil, false
|
||||
}
|
||||
@@ -59,14 +59,14 @@ func keepListing(files []file, limit int, price func(string) *catalog.Price, tit
|
||||
for i, f := range files {
|
||||
k.files[i] = keptFileOf(f)
|
||||
}
|
||||
k.prices, k.titles, k.desktop = map[string]*catalog.Price{}, map[string]string{}, map[string]string{}
|
||||
k.prices, k.titles, k.desktop = map[string]*catalog.Price{}, titleAnswers{}, map[string]string{}
|
||||
return func(model string) *catalog.Price {
|
||||
p := price(model)
|
||||
k.prices[model] = p
|
||||
return p
|
||||
}, func(id string) string {
|
||||
t := titles(id)
|
||||
k.titles[id] = t
|
||||
k.titles.note(id, t)
|
||||
return t
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,7 @@ func keepList(out []Session) {
|
||||
if i := slices.IndexFunc(out, func(s Session) bool { return s.Agent == "agy" && s.WSL == "" }); i >= 0 {
|
||||
keptList.agy, keptList.agyStart = true, agyStart()
|
||||
}
|
||||
keptList.titles.settle()
|
||||
keptList.out = slices.Clone(out)
|
||||
}
|
||||
|
||||
@@ -103,5 +104,5 @@ func agyStart() string {
|
||||
|
||||
func forgetKeptList() {
|
||||
keptList.files, keptList.out = nil, nil
|
||||
keptList.prices, keptList.titles, keptList.desktop = nil, nil, nil
|
||||
keptList.prices, keptList.titles, keptList.desktop = nil, titleAnswers{}, nil
|
||||
}
|
||||
|
||||
@@ -320,6 +320,7 @@ func statsKept(ranges []int, now time.Time, wantNative bool) ([]Stats, map[strin
|
||||
keepRange(ranges[i], st, ranges)
|
||||
out[i] = st
|
||||
}
|
||||
keptStats.titles.settle()
|
||||
return out, keptStats.native
|
||||
}
|
||||
|
||||
|
||||
@@ -25,10 +25,51 @@ var keptStats struct {
|
||||
// the answers the reads got from the prices, Codex's names and Claude
|
||||
// Desktop's, asked again before the ranges are given out
|
||||
prices map[string]*catalog.Price
|
||||
titles map[string]string
|
||||
titles titleAnswers
|
||||
desktop map[string]string
|
||||
}
|
||||
|
||||
// titleAnswers are the answers a titles func gave, to be asked again
|
||||
// (keptStill). Nearly every id has no title, and a map of every session's
|
||||
// id held 3MB for a history of 18k files (#2278): an id answered "" is
|
||||
// only listed, sorted and without repeats once settled.
|
||||
type titleAnswers struct {
|
||||
named map[string]string
|
||||
none []string
|
||||
}
|
||||
|
||||
func (a *titleAnswers) note(id, t string) {
|
||||
if t == "" {
|
||||
a.none = append(a.none, id)
|
||||
return
|
||||
}
|
||||
if a.named == nil {
|
||||
a.named = map[string]string{}
|
||||
}
|
||||
a.named[id] = t
|
||||
}
|
||||
|
||||
// settle sorts none and drops its repeats, and its slack.
|
||||
func (a *titleAnswers) settle() {
|
||||
slices.Sort(a.none)
|
||||
a.none = slices.Clip(slices.Compact(a.none))
|
||||
}
|
||||
|
||||
// still reports whether titles answers each id as it did.
|
||||
func (a *titleAnswers) still(titles func(string) string) bool {
|
||||
for id, t := range a.named {
|
||||
if titles(id) != t {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for i, id := range a.none {
|
||||
if (i == 0 || id != a.none[i-1]) && titles(id) != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// keptFile is what of a listed file a range's stats are made from: its
|
||||
// parse (a published parse is never written, a changed file gets a new
|
||||
// one), and what the listing says of it.
|
||||
@@ -51,14 +92,14 @@ func keptFileOf(f file) keptFile {
|
||||
func keptFor(files []file, today time.Time, price func(string) *catalog.Price, titles func(string) string) bool {
|
||||
k := &keptStats
|
||||
same := k.ranges != nil && k.gen == cacheGeneration && k.today.Equal(today) &&
|
||||
keptStill(k.files, files, k.prices, k.titles, k.desktop, price, titles)
|
||||
keptStill(k.files, files, k.prices, &k.titles, k.desktop, price, titles)
|
||||
if !same {
|
||||
forgetKeptStats()
|
||||
k.files, k.today, k.gen = make([]keptFile, len(files)), today, cacheGeneration
|
||||
for i, f := range files {
|
||||
k.files[i] = keptFileOf(f)
|
||||
}
|
||||
k.ranges, k.prices, k.titles, k.desktop = map[int]Stats{}, map[string]*catalog.Price{}, map[string]string{}, map[string]string{}
|
||||
k.ranges, k.prices, k.titles, k.desktop = map[int]Stats{}, map[string]*catalog.Price{}, titleAnswers{}, map[string]string{}
|
||||
}
|
||||
return same
|
||||
}
|
||||
@@ -66,7 +107,7 @@ func keptFor(files []file, today time.Time, price func(string) *catalog.Price, t
|
||||
// keptStill reports whether kept was taken of files as they are now, and
|
||||
// prices, titles and desktop, the answers what was made from them got, are
|
||||
// still what price, titles and Claude Desktop's names answer.
|
||||
func keptStill(kept []keptFile, files []file, prices map[string]*catalog.Price, names, desktop map[string]string,
|
||||
func keptStill(kept []keptFile, files []file, prices map[string]*catalog.Price, names *titleAnswers, desktop map[string]string,
|
||||
price func(string) *catalog.Price, titles func(string) string) bool {
|
||||
if len(kept) != len(files) {
|
||||
return false
|
||||
@@ -81,10 +122,8 @@ func keptStill(kept []keptFile, files []file, prices map[string]*catalog.Price,
|
||||
return false
|
||||
}
|
||||
}
|
||||
for id, t := range names {
|
||||
if titles(id) != t {
|
||||
return false
|
||||
}
|
||||
if !names.still(titles) {
|
||||
return false
|
||||
}
|
||||
if len(desktop) > 0 {
|
||||
ids := make([]string, 0, len(desktop))
|
||||
@@ -139,7 +178,7 @@ func keptPrice(price func(string) *catalog.Price) func(string) *catalog.Price {
|
||||
func keptTitle(titles func(string) string) func(string) string {
|
||||
return func(id string) string {
|
||||
t := titles(id)
|
||||
keptStats.titles[id] = t
|
||||
keptStats.titles.note(id, t)
|
||||
return t
|
||||
}
|
||||
}
|
||||
@@ -163,5 +202,5 @@ func keepDesktop(st Stats) {
|
||||
|
||||
func forgetKeptStats() {
|
||||
keptStats.files, keptStats.ranges, keptStats.native = nil, nil, nil
|
||||
keptStats.prices, keptStats.titles, keptStats.desktop = nil, nil, nil
|
||||
keptStats.prices, keptStats.titles, keptStats.desktop = nil, titleAnswers{}, nil
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -23,7 +25,7 @@ func statsOutput(st Stats) (n int, cost float64) {
|
||||
// the ranges the last one read, not each put together again (#2278), and a
|
||||
// file that changed, or a price, is read into them.
|
||||
func TestStatsKeptBetweenPolls(t *testing.T) {
|
||||
claude, _ := setup(t)
|
||||
claude, codex := setup(t)
|
||||
ranges := []int{0, 7, 30}
|
||||
first, native := StatsAndNativeAll(ranges)
|
||||
if len(first[0].Sessions) == 0 || len(native) == 0 {
|
||||
@@ -60,6 +62,41 @@ func TestStatsKeptBetweenPolls(t *testing.T) {
|
||||
if _, c := statsOutput(repriced[0]); c <= cost {
|
||||
t.Fatalf("cost %v after the prices went up, %v before", c, cost)
|
||||
}
|
||||
|
||||
// a thread with no name when the ranges were read, named in Codex since
|
||||
const cx = "01a0bdc9-b5fd-7e63-9658-68bc8dce5ecd"
|
||||
line := fmt.Sprintf("{\"id\":%q,\"thread_name\":\"Named in Codex\",\"updated_at\":%q}\n", cx, time.Now().UTC().Format(time.RFC3339Nano))
|
||||
if err := os.WriteFile(filepath.Join(codex, "session_index.jsonl"), []byte(line), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
named, _ := StatsAndNativeAll(ranges)
|
||||
i := slices.IndexFunc(named[0].Sessions, func(s Summary) bool { return s.ID == cx })
|
||||
if i < 0 || named[0].Sessions[i].Title != "Named in Codex" {
|
||||
t.Fatalf("Codex's thread after it was named: %d %+v", i, named[0].Sessions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTitleAnswersSettled: the ids a title was asked for and none given are
|
||||
// held once each, at their length, and asked again.
|
||||
func TestTitleAnswersSettled(t *testing.T) {
|
||||
var a titleAnswers
|
||||
for _, id := range []string{"c", "a", "b", "a", "c", "c"} {
|
||||
a.note(id, "")
|
||||
}
|
||||
a.note("n", "Named")
|
||||
a.settle()
|
||||
if !slices.Equal(a.none, []string{"a", "b", "c"}) || cap(a.none) != len(a.none) {
|
||||
t.Fatalf("none %v, cap %d", a.none, cap(a.none))
|
||||
}
|
||||
if !a.still(func(id string) string { return map[string]string{"n": "Named"}[id] }) {
|
||||
t.Fatal("unchanged answers read as changed")
|
||||
}
|
||||
if a.still(func(id string) string { return map[string]string{"n": "Named", "b": "B"}[id] }) {
|
||||
t.Fatal("an id named since read as unchanged")
|
||||
}
|
||||
if a.still(func(string) string { return "" }) {
|
||||
t.Fatal("a name taken away read as unchanged")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatsKeptNewDay: the day turning over reads the ranges again, as
|
||||
|
||||
Reference in New Issue
Block a user