Skip to content

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

sessions: a long Codex session being written is read on from where it was, not from its first line

yetone3b3bc359da8d
diff --git a/internal/sessions/calls_test.go b/internal/sessions/calls_test.go
index 14ee5e16f..b95d4c151 100644
--- a/internal/sessions/calls_test.go
+++ b/internal/sessions/calls_test.go
@@ -18,7 +18,7 @@ var callT0 = time.Date(2026, 9, 20, 10, 0, 0, 0, time.UTC)
// callDirs are the folders a test's calls are read from.
type callDirs struct{ claude, codex, desktop string }
-func setupCalls(t *testing.T) callDirs {
+func setupCalls(t testing.TB) callDirs {
t.Helper()
dir := t.TempDir()
d := callDirs{filepath.Join(dir, "claude"), filepath.Join(dir, "codex"), filepath.Join(dir, "desktop")}
diff --git a/internal/sessions/codex_long_session_test.go b/internal/sessions/codex_long_session_test.go
new file mode 100644
index 000000000..dea63aa78
--- /dev/null
+++ b/internal/sessions/codex_long_session_test.go
@@ -0,0 +1,166 @@
+package sessions
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+// longRollout is calls [from, to) of a Codex session: each a turn with the
+// user's message, a shell call, its 4 KB output, the usage line and the
+// turn's end, as a working rollout writes them.
+func longRollout(from, to int) []string {
+ var lines []string
+ if from == 0 {
+ lines = append(lines, `{"timestamp":"`+stamp(0)+`","type":"session_meta","payload":{"id":"0190aaaa-1111-7222-8333-444455556666","cwd":"/work/it"}}`)
+ }
+ out := strings.Repeat("x", 4000)
+ for i := from; i < to; i++ {
+ turn := fmt.Sprintf("t%d", i)
+ lines = append(lines, cxTurnLines(i*3, turn, "gpt-6-astra", "high")...)
+ lines = append(lines,
+ fmt.Sprintf(`{"timestamp":"%s","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"step %d"}]}}`, stamp(i*3), i),
+ fmt.Sprintf(`{"timestamp":"%s","type":"response_item","payload":{"type":"function_call","name":"shell","arguments":"{}","call_id":"c%d"}}`, stamp(i*3+1), i),
+ fmt.Sprintf(`{"timestamp":"%s","type":"response_item","payload":{"type":"function_call_output","call_id":"c%d","output":"%s"}}`, stamp(i*3+1), i, out),
+ tokenCountLine(i*3+2, cxUse(100*(i+1), 40*(i+1), 0, 10*(i+1), 2*(i+1)), cxUse(100, 40, 0, 10, 2)),
+ cxDoneLine(i*3+2, turn, 1000, 100),
+ )
+ }
+ return lines
+}
+
+// longSessionCalls is past the shared revision budget: about six index
+// entries a call (yetone/magpie-releases#24's session was longer still).
+const longSessionCalls = 2000
+
+// TestLongCodexSessionAppendReadsOnlyTheAppend: a Codex session being
+// written, longer than the shared revision budget, is read on from where
+// the last read ended, by the Requests reader and the summary alike. Before
+// #24 each append parsed the whole rollout again from its first line.
+func TestLongCodexSessionAppendReadsOnlyTheAppend(t *testing.T) {
+ d := setupCalls(t)
+ path := filepath.Join(d.codex, "sessions", "rollout-2026-09-20T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
+ writeLines(t, path, longRollout(0, longSessionCalls)...)
+ var mu sync.Mutex
+ var starts []int64
+ scanHook = func(p string, off int64) {
+ if p == path {
+ mu.Lock()
+ starts = append(starts, off)
+ mu.Unlock()
+ }
+ }
+ t.Cleanup(func() { scanHook = nil })
+ read := func() ([]Call, Session) {
+ t.Helper()
+ var calls []Call
+ for _, s := range CallSources() {
+ if s.Path == path {
+ calls = ReadCallSource(s)
+ }
+ }
+ ss := List(0)
+ if len(ss) != 1 {
+ t.Fatalf("%d sessions", len(ss))
+ }
+ return calls, ss[0]
+ }
+ if cs, _ := read(); len(cs) != longSessionCalls {
+ t.Fatalf("%d calls", len(cs))
+ }
+ if c, ok := callContinuations[path]; ok && c.state.revisionWeight() <= revisionEntries {
+ w := c.state.revisionWeight()
+ t.Fatalf("index weight %d is inside the shared budget; the fixture is not long enough", w)
+ }
+ var calls []Call
+ var summary Session
+ for k := 0; k < 3; k++ {
+ st, err := os.Stat(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ appendText(t, path, strings.Join(longRollout(longSessionCalls+k, longSessionCalls+k+1), "\n")+"\n")
+ mu.Lock()
+ starts = nil
+ mu.Unlock()
+ calls, summary = read()
+ if len(calls) != longSessionCalls+k+1 {
+ t.Fatalf("append %d: %d calls", k, len(calls))
+ }
+ mu.Lock()
+ got := append([]int64(nil), starts...)
+ mu.Unlock()
+ if len(got) == 0 {
+ t.Fatalf("append %d: not read", k)
+ }
+ for _, off := range got {
+ if off != st.Size() {
+ t.Fatalf("append %d: read from %d, not where the last read ended (%d): %v", k, off, st.Size(), got)
+ }
+ }
+ }
+
+ // What was read on is what a fresh read of the whole file gives.
+ scanHook = nil
+ Reset()
+ if err := os.RemoveAll(filepath.Dir(CachePath())); err != nil {
+ t.Fatal(err)
+ }
+ fresh, freshSummary := read()
+ for i := range calls {
+ if !reflect.DeepEqual(calls[i], fresh[i]) {
+ t.Fatalf("call %d read on:\n%+v\nread fresh:\n%+v", i, calls[i], fresh[i])
+ }
+ }
+ if len(calls) != len(fresh) {
+ t.Fatalf("%d calls read on, %d fresh", len(calls), len(fresh))
+ }
+ if !reflect.DeepEqual(summary, freshSummary) {
+ t.Fatalf("summary read on:\n%+v\nread fresh:\n%+v", summary, freshSummary)
+ }
+}
+
+// BenchmarkLongCodexSessionAppend is one turn appended to a long Codex
+// session, then read by the Requests reader and the summary, as a tray poll
+// does while Codex runs (yetone/magpie-releases#24).
+func BenchmarkLongCodexSessionAppend(b *testing.B) {
+ d := setupCalls(b)
+ path := filepath.Join(d.codex, "sessions", "rollout-2026-09-20T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ b.Fatal(err)
+ }
+ if err := os.WriteFile(path, []byte(strings.Join(longRollout(0, longSessionCalls), "\n")+"\n"), 0o644); err != nil {
+ b.Fatal(err)
+ }
+ read := func() {
+ for _, s := range CallSources() {
+ if s.Path == path {
+ ReadCallSource(s)
+ }
+ }
+ List(0)
+ }
+ read()
+ f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o644)
+ if err != nil {
+ b.Fatal(err)
+ }
+ defer f.Close()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ b.StopTimer()
+ if _, err := f.WriteString(strings.Join(longRollout(longSessionCalls+i, longSessionCalls+i+1), "\n") + "\n"); err != nil {
+ b.Fatal(err)
+ }
+ // a later mtime, as a real append a moment after the last has
+ at := time.Now().Add(time.Duration(i+1) * time.Millisecond)
+ _ = os.Chtimes(path, at, at)
+ b.StartTimer()
+ read()
+ }
+}
diff --git a/internal/sessions/revision_cache.go b/internal/sessions/revision_cache.go
index b77eef216..c1d5a8ee2 100644
--- a/internal/sessions/revision_cache.go
+++ b/internal/sessions/revision_cache.go
@@ -13,6 +13,17 @@ const revisionIdle = 5 * time.Minute
const revisionFiles = 8
const revisionEntries = 8192
+// A Codex session being written keeps its window even past the shared
+// budget. Its index holds every call (about six entries each), so a session
+// over ~1,400 calls lost it, and every append (each turn, each poll that
+// read it) parsed the whole rollout again from its first line: a full core
+// while Codex ran (yetone/magpie-releases#24). The full parse built the same
+// index each time anyway, so keeping it costs no more at its peak. At most
+// revisionActiveFiles such windows, each up to revisionActiveEntries, and
+// only while the file changed in the last revisionIdle.
+const revisionActiveFiles = 2
+const revisionActiveEntries = 16 * revisionEntries
+
// A pause longer than five minutes must not demote a large working session
// behind tiny subagent logs. After an hour, recent work takes priority; idle
// Claude windows are not discarded unless that capacity is actually needed.
@@ -23,7 +34,7 @@ func workingRevision(mod int64, now time.Time) bool {
}
func recentRevision(mod int64, weight int, now time.Time) bool {
- return weight > 0 && weight <= revisionEntries && mod >= now.Add(-revisionIdle).UnixNano()
+ return weight > 0 && weight <= revisionActiveEntries && mod >= now.Add(-revisionIdle).UnixNano()
}
func (s *codexUsageState) weight() int {
@@ -126,9 +137,20 @@ func retainedRevisions(candidates []revisionCandidate, now time.Time) map[string
return candidates[i].path < candidates[j].path
})
keep := map[string]bool{}
- used := 0
+ used, active := 0, 0
for _, c := range candidates {
- if !c.eligible(now) || len(keep) == revisionFiles || used+c.weight > revisionEntries {
+ if !c.eligible(now) || len(keep) == revisionFiles {
+ continue
+ }
+ if !c.bounded && c.weight > revisionEntries {
+ // an active Codex session past the shared budget: outside it
+ if active < revisionActiveFiles {
+ keep[c.path] = true
+ active++
+ }
+ continue
+ }
+ if used+c.weight > revisionEntries {
continue
}
keep[c.path] = true
diff --git a/internal/sessions/revision_cache_test.go b/internal/sessions/revision_cache_test.go
index 9d5933696..99953004a 100644
--- a/internal/sessions/revision_cache_test.go
+++ b/internal/sessions/revision_cache_test.go
@@ -105,7 +105,15 @@ 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)
- if keep["large"] || len(keep) != 2 {
+ // 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)
}
}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 153337647..5afb21a51 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -1241,6 +1241,9 @@ func scan(path string, off int64, fn func([]byte)) (int64, error) {
// to is stepped over without being gathered (most of a Codex rollout's
// bytes are lines no one reads: compactions, tool output). A nil head
// wants every line.
+// scanHook, when a test sets it, is told where each scan of a file starts.
+var scanHook func(path string, off int64)
+
func scanHead(path string, off int64, head func([]byte) bool, fn func([]byte)) (int64, error) {
return scanAt(path, off, head, func(b []byte, _, _ int64) bool { fn(b); return true })
}
@@ -1251,6 +1254,9 @@ func scanHead(path string, off int64, head func([]byte) bool, fn func([]byte)) (
// A compressed file's places are those of its lines decompressed; it is read
// from its start, what comes before off passed over.
func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, start, end int64) bool) (int64, error) {
+ if scanHook != nil {
+ scanHook(path, off)
+ }
f, err := openLines(path)
if err != nil {
return off, err