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 at9fd80b212plus 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
This commit is contained in:
+62
-1
@@ -13,6 +13,8 @@ import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
satomic "sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
@@ -1869,13 +1871,72 @@ func codexChatGPT(dir string) bool {
|
||||
// newest day of it at most — from the log database the Codex app and the
|
||||
// TUI share: the address it opened, and whether that was refused (nothing
|
||||
// listening there). Zero when none is logged.
|
||||
//
|
||||
// Every Drift asks it, so it is read again only when the database or its
|
||||
// -wal changed, or the day it covers moved past what it found (#1665: a
|
||||
// read per ask was ~2,500 opens of logs_2.sqlite in 25s of a start, each
|
||||
// one file system work a Windows filter driver adds its own to).
|
||||
func codexReached(dir string, since time.Time) (at time.Time, to string, refused bool) {
|
||||
logs, _ := filepath.Glob(filepath.Join(dir, "logs_*.sqlite"))
|
||||
if len(logs) == 0 {
|
||||
return
|
||||
}
|
||||
slices.Sort(logs)
|
||||
db, err := provider.OpenReadOnly(logs[len(logs)-1])
|
||||
path := logs[len(logs)-1]
|
||||
key := reachedKey{path: path, since: since.Unix(), db: stampOf(path), wal: stampOf(path + "-wal")}
|
||||
reachedMu.Lock()
|
||||
if c, ok := reachedMemo[dir]; ok && c.key == key && time.Since(c.read) < reachedFor &&
|
||||
(c.at.IsZero() || time.Since(c.at) < 24*time.Hour) {
|
||||
reachedMu.Unlock()
|
||||
return c.at, c.to, c.refused
|
||||
}
|
||||
reachedMu.Unlock()
|
||||
at, to, refused = codexReachedRead(path, since)
|
||||
reachedMu.Lock()
|
||||
reachedMemo[dir] = reachedRead{key: key, read: time.Now(), at: at, to: to, refused: refused}
|
||||
reachedMu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// reachedFor is how long a read of Codex's log stands while its files say
|
||||
// nothing changed: a file system whose times are coarse can miss a write
|
||||
// that kept the size.
|
||||
const reachedFor = 15 * time.Second
|
||||
|
||||
type reachedKey struct {
|
||||
path string
|
||||
since int64
|
||||
db, wal fileStamp
|
||||
}
|
||||
|
||||
type fileStamp struct{ size, mod int64 }
|
||||
|
||||
func stampOf(path string) fileStamp {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return fileStamp{size: -1}
|
||||
}
|
||||
return fileStamp{size: fi.Size(), mod: fi.ModTime().UnixNano()}
|
||||
}
|
||||
|
||||
type reachedRead struct {
|
||||
key reachedKey
|
||||
read time.Time
|
||||
at time.Time
|
||||
to string
|
||||
refused bool
|
||||
}
|
||||
|
||||
var (
|
||||
reachedMu sync.Mutex
|
||||
reachedMemo = map[string]reachedRead{}
|
||||
// reachedOpens counts the reads of the database, for the tests
|
||||
reachedOpens satomic.Int64
|
||||
)
|
||||
|
||||
func codexReachedRead(path string, since time.Time) (at time.Time, to string, refused bool) {
|
||||
reachedOpens.Add(1)
|
||||
db, err := provider.OpenReadOnly(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user