Files
magpie/internal/agent/codex_reached_test.go
T
yetone 84210ae7bd agent: Codex's log database is read again only when it or its -wal changed
#1665, owner's item 2: codexReached opened Codex's logs_*.sqlite on
every ask, and every Drift asks it. The report's trace shows ~2465 reads
of logs_2.sqlite in 25s of a Windows start. The read is now kept per
Codex folder. The key is the database path, the since day, and the size
and modification time of the database and its -wal. A kept read stands
for at most 15s, so a file system with coarse times can't hide a write
that kept the size. A hit older than 24h is never kept, since it falls
out of the window being read. In an interposed sandbox start, opens of
logs_2.sqlite went from 6-8 per run to 4.

TestCodexReachedReadsLogOnlyWhenItChanged asks 50 times and expects 1
read. It then writes to the log and expects a fresh read that sees the
write. With the cache check turned off by hand, it goes red:
  --- FAIL: TestCodexReachedReadsLogOnlyWhenItChanged (3.23s)
      codex_reached_test.go:50: 50 asks read the log 50 times, want 1

Owner's item 3: the PATH x PATHEXT lookup cache, 8bfe33f1f (#1771), is
an ancestor of this commit and shipped in v0.1.1214. The reporter's
trace was taken on v0.1.1212.

What was run (worktree at 9fd80b212 plus this change and the commits
before it):
  gofmt -l internal/: nothing
  go vet ./internal/agent: exit 0
  GOOS=linux go vet -tags nogui ./...: exit 0
  GOOS=windows go vet -tags nogui ./...: exit 0
  go test -timeout 45m -count=1 ./internal/agent: ok 2591.806s
2026-10-20 20:23:55 +08:00

67 lines
2.3 KiB
Go

package agent
import (
"database/sql"
"os"
"path/filepath"
"testing"
"time"
)
// #1665: Drift asks where Codex's requests went on every poll, and each ask
// opened logs_*.sqlite again (~2,500 opens in 25s of a start on Windows,
// each one work a filter driver multiplies). The log is read again only
// once it changed; asked again meanwhile, the answer is the one read.
func TestCodexReachedReadsLogOnlyWhenItChanged(t *testing.T) {
home, _ := codexHome(t, "", "")
dir := filepath.Join(home, ".codex")
os.MkdirAll(dir, 0o700)
db, err := sql.Open("sqlite", filepath.Join(dir, "logs_2.sqlite"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`PRAGMA journal_mode=WAL`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE logs (id INTEGER PRIMARY KEY AUTOINCREMENT, ts INTEGER NOT NULL, ts_nanos INTEGER NOT NULL,
level TEXT NOT NULL, target TEXT NOT NULL, feedback_log_body TEXT)`); err != nil {
t.Fatal(err)
}
at := time.Now().Add(-time.Minute).Unix()
log := func(url string) {
at++
if _, err := db.Exec(`INSERT INTO logs (ts, ts_nanos, level, target, feedback_log_body) VALUES (?, 0, 'INFO', 'codex_api::endpoint::responses_websocket', ?)`,
at, "responses_websocket.connect{}: connecting to websocket: "+url); err != nil {
t.Fatal(err)
}
}
log("ws://127.0.0.1:3425/backend-api/codex/responses")
since := time.Now().Add(-time.Hour)
before := reachedOpens.Load()
for range 50 {
got, to, _ := codexReached(dir, since)
if got.Unix() != at || to != "ws://127.0.0.1:3425/backend-api/codex/responses" {
t.Fatalf("read %v %q, want %v and magpie's address", got, to, at)
}
}
if n := reachedOpens.Load() - before; n != 1 {
t.Fatalf("50 asks read the log %d times, want 1", n)
}
// a new request logged is read on the next ask
log("wss://chatgpt.com/backend-api/codex/responses")
if got, to, _ := codexReached(dir, since); got.Unix() != at || to != "wss://chatgpt.com/backend-api/codex/responses" {
t.Fatalf("after a new request: %v %q", got, to)
}
if n := reachedOpens.Load() - before; n != 2 {
t.Fatalf("the log read %d times, want 2 once it changed", n)
}
// asked from a later time, the answer is read for that time
if got, _, _ := codexReached(dir, time.Now().Add(time.Minute)); !got.IsZero() {
t.Fatalf("since after every request: %v", got)
}
}