yetone/magpie-releases#24 (LeiCheng42, v0.1.1280): a 30s profile while Codex ran shows magpie at about a core, ~24% of it in usage.prefetchSources -> sessions.ReadCallSource -> readCalls -> scanAt. The tray's usage loop (watchTrayUsage -> usage.QueryPage(Today)), the usage API and the Sessions page read every source whose size or mtime changed, and an active Codex rollout changes each turn. A Codex continuation's index weighs about six entries a call, so a session past ~1,365 calls was over the shared 8192-entry revision budget: its window was dropped, and every append parsed the whole rollout again from offset 0, for the Requests reader and the summary (List) alike. Now up to two (revisionActiveFiles) recently written Codex sessions keep their window past the shared budget, each up to 16x it (131,072 entries), and only while the file changed in the last revisionIdle (5 min). The full parse built the same index on every append anyway, so this costs no more at its peak; an idle session still loses it. Measured (Apple M4 Pro, BenchmarkLongCodexSessionAppend: a 2,000-call, 10.7 MB rollout, one turn appended, then ReadCallSource + List(0)): before: 0.87-1.09 s/op after: 26-60 ms/op TestLongCodexSessionAppendReadsOnlyTheAppend (via a scanAt hook) checks that each of three appends is read from the previous size by both readers, and that the result DeepEquals a fresh read of the whole file (calls and summary) after the caches are dropped. On the old code it fails: "append 0: read from 0, not where the last read ended (10691427)". TestRevisionEntryBudget covers the two-window cap and the idle/oversize drops. Run: go test -tags nogui ./internal/sessions/ ok; go test -tags nogui -timeout 45m ./... with the redact change on top, under a load average of 440-610 from other sessions' suites: 59 ok, and agent and gateway (45m timeout), gui (TestSetAnswersWhileDevinAndProvidersHang, set took 1m30s), edit (TestTOMLBigFileParsesInLinearTime, 1.0s) and middleware (TestStream, onEvent "took too long") failed on time. edit and middleware rerun alone: ok. agent and gateway rerun at a lower load: ok; gui rerun: ok. (The first rerun had TestPluginStreamLargeReplies red on time again, #2551, which passes alone in 22s, and TestDiffLines at 2.04s against its 2s bound, filed as #2766.) gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./... ok.
158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSettledCodexRevisionsReleasedAndRebuilt(t *testing.T) {
|
|
u := cxUse(100, 40, 0, 10, 2)
|
|
path := codexUsageLog(t, append(cxTurnLines(0, "t1", "m", "high"), cxRecordLine(1, "a", u, u), tokenCountLine(2, u, u)))
|
|
old := time.Now().Add(-time.Hour)
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
List(0)
|
|
if cache[path] == nil || cache[path].Codex != nil {
|
|
t.Fatal("settled summary retained its response index")
|
|
}
|
|
if cs := Calls(time.Time{}); len(cs) != 1 {
|
|
t.Fatal(cs)
|
|
}
|
|
if callCache[path].CX != nil {
|
|
t.Fatal("settled calls retained their response index")
|
|
}
|
|
for _, source := range CallSources() {
|
|
ReadCallSource(source)
|
|
}
|
|
if callCache[path] != nil {
|
|
t.Fatal("source reader retained expanded rows after index eviction")
|
|
}
|
|
if len(callContinuations) != 0 {
|
|
t.Fatal("settled source retained continuation")
|
|
}
|
|
appendText(t, path, tokenCountLine(3, cxUse(200, 80, 0, 20, 4), u)+"\n")
|
|
if cs := Calls(time.Time{}); len(cs) != 2 {
|
|
t.Fatal("reopened file lost/doubled calls", cs)
|
|
}
|
|
if ss := List(0); len(ss) != 1 || ss[0].Output != 20 {
|
|
t.Fatal("reopened summary", ss)
|
|
}
|
|
}
|
|
|
|
func TestRevisionCachesBoundActiveFilesAndExpire(t *testing.T) {
|
|
d := setupCalls(t)
|
|
u := cxUse(100, 40, 0, 10, 2)
|
|
for i := 0; i < revisionFiles+5; i++ {
|
|
path := filepath.Join(d.codex, "sessions", fmt.Sprintf("rollout-2026-09-20T10-00-00-%08d-1111-7222-8333-444455556666.jsonl", i))
|
|
writeLines(t, path, append(cxTurnLines(0, "t1", "m", "high"), cxRecordLine(1, fmt.Sprint(i), u, u), tokenCountLine(2, u, u))...)
|
|
}
|
|
List(0)
|
|
Calls(time.Time{})
|
|
n := 0
|
|
for _, s := range cache {
|
|
if s.Codex != nil {
|
|
n++
|
|
}
|
|
}
|
|
if n != revisionFiles {
|
|
t.Fatalf("summary retained %d indexes", n)
|
|
}
|
|
for _, source := range CallSources() {
|
|
ReadCallSource(source)
|
|
}
|
|
if len(callContinuations) > revisionFiles {
|
|
t.Fatal(len(callContinuations))
|
|
}
|
|
// Published snapshots may still be in use by a concurrent parser.
|
|
var snapshot *state
|
|
for _, s := range cache {
|
|
if s.Codex != nil {
|
|
snapshot = s
|
|
break
|
|
}
|
|
}
|
|
mu.Lock()
|
|
trimSummaryRevisions(time.Now().Add(revisionIdle + time.Second))
|
|
mu.Unlock()
|
|
if snapshot.Codex == nil {
|
|
t.Fatal("mutated a published snapshot")
|
|
}
|
|
for _, s := range cache {
|
|
if s.Codex != nil {
|
|
t.Fatal("idle summary retained index")
|
|
}
|
|
}
|
|
callsMu.Lock()
|
|
trimCallRevisions(time.Now().Add(revisionIdle + time.Second))
|
|
callsMu.Unlock()
|
|
if len(callContinuations) != 0 {
|
|
t.Fatal("idle continuations retained")
|
|
}
|
|
for _, s := range callCache {
|
|
if s.CX != nil {
|
|
t.Fatal("idle call cache retained index")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRevisionEntryBudget(t *testing.T) {
|
|
now := time.Now()
|
|
candidates := []revisionCandidate{{path: "large", mod: now.UnixNano(), weight: revisionEntries + 1}, {path: "one", mod: now.UnixNano(), weight: revisionEntries / 2}, {path: "two", mod: now.UnixNano(), weight: revisionEntries / 2}, {path: "three", mod: now.UnixNano(), weight: revisionEntries / 2}}
|
|
keep := retainedRevisions(candidates, now)
|
|
// an active Codex session past the shared budget keeps its own window
|
|
// (yetone/magpie-releases#24); the rest still share the budget
|
|
if !keep["large"] || len(keep) != 3 {
|
|
t.Fatal(keep)
|
|
}
|
|
// at most revisionActiveFiles of them, each up to revisionActiveEntries
|
|
candidates = []revisionCandidate{{path: "a", mod: now.UnixNano(), weight: revisionEntries + 3}, {path: "b", mod: now.UnixNano(), weight: revisionEntries + 2}, {path: "c", mod: now.UnixNano(), weight: revisionEntries + 1}, {path: "huge", mod: now.UnixNano(), weight: revisionActiveEntries + 1}, {path: "idle", mod: now.Add(-revisionIdle - time.Minute).UnixNano(), weight: revisionEntries + 4}}
|
|
keep = retainedRevisions(candidates, now)
|
|
if !keep["a"] || !keep["b"] || len(keep) != revisionActiveFiles {
|
|
t.Fatal(keep)
|
|
}
|
|
}
|
|
|
|
func TestCallFrameOmitsRuntimeTypeGraph(t *testing.T) {
|
|
var b bytes.Buffer
|
|
if err := gob.NewEncoder(&b).Encode(callFrame{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"codexUsageState", "codexContribution", "claudeUsageState", "cxRun", "callFile"} {
|
|
if bytes.Contains(b.Bytes(), []byte(name)) {
|
|
t.Fatalf("runtime type %s persisted", name)
|
|
}
|
|
}
|
|
// Main's empty frame is about 1.3 KB; the previous PR exceeded 2.3 KB.
|
|
if b.Len() > 1300 {
|
|
t.Fatalf("empty frame grew to %d bytes", b.Len())
|
|
}
|
|
t.Logf("empty frame: %d bytes", b.Len())
|
|
}
|
|
|
|
func TestCodexCounterResetAtNonIncreasingTime(t *testing.T) {
|
|
for _, second := range []int{10, 5} {
|
|
t.Run(fmt.Sprint(second), func(t *testing.T) {
|
|
u := cxUse(100, 40, 0, 10, 2)
|
|
small := cxUse(40, 10, 0, 4, 1)
|
|
lines := append(cxTurnLines(0, "t1", "m", "high"), tokenCountLine(10, u, u), tokenCountLine(second, small, small), tokenCountLine(second+1, cxUse(80, 20, 0, 8, 2), small))
|
|
codexUsageLog(t, lines)
|
|
for _, restart := range []bool{false, true} {
|
|
if restart {
|
|
Reset()
|
|
}
|
|
cs := Calls(time.Time{})
|
|
ss := List(0)
|
|
if len(cs) != 3 || len(ss) != 1 || ss[0].Output != 18 {
|
|
t.Fatalf("restart=%v calls=%+v summary=%+v", restart, cs, ss)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|