Files
magpie/internal/agent/codex_reached_test.go
T
JoeyWu-tech b8de6e5afd agent: TestCodexDriftReached and TestCodexBehindReverseProxy read Codex's log again after each request they log (#3398)
codexReached keeps its read of Codex's log while the database and its
-wal keep their size and modification time (84210ae7, #1665), for up to
reachedFor (15s). Both tests insert a request into logs_2.sqlite and ask
Drift at once, three or four times over. Two inserts inside one tick of
the file system's clock leave that stamp as it was, so Drift answers
from the read taken before the second insert. On this Linux box (ext4
under /tmp), 317 of 400 back-to-back inserts left a rollback-journal
database's size and time unchanged; in WAL mode 156 of 400 did, once
the -wal had wrapped and was written over in place. Codex's own
logs_2.sqlite here is WAL, with a -wal that stays at 4.4 MB.

On b437e4ad, -count=60 of the two tests:
  TestCodexDriftReached red 11 times
    applied_test.go:280: elsewhere: &{Kind:bypassed ... Detail:Codex couldn't reach magpie at 09:46 — magpie wasn't running then, ...} (5)
    applied_test.go:284: reached magpie: &{Kind:bypassed ... Detail:Codex's last request (09:46) went to chatgpt.com, not magpie — ...} (6)
  TestCodexBehindReverseProxy red 2 times
    proxy_kept_test.go:293: to chatgpt.com: <nil>
Both tests are older than the memo; before it, each ask opened the log.

These tests are about what Drift says of each kind of request. When the
log is read again is TestCodexReachedReadsLogOnlyWhenItChanged's to
test. So each request they log now drops the kept read
(forgetReached), and the next ask reads the log.

What was run (tree d5c248e9, on b437e4ad):
  the tick injected: after each insert, the database's time set back
  to what it was before the insert (os.Chtimes), so every insert keeps
  the stamp. -count=20 of both tests:
    without this change: both red 20 of 20, at the elsewhere check
    (applied_test.go:280) and the to chatgpt.com one
    (proxy_kept_test.go:293)
    with it: ok
  -count=200 of TestCodexDriftReached, TestCodexBehindReverseProxy and
  TestCodexReachedReadsLogOnlyWhenItChanged: ok
  broken by hand, with this change:
    codexReachedRead never records a refusal:
      applied_test.go:277: refused: <nil>
    applied.go's went-elsewhere case off:
      applied_test.go:281: elsewhere: <nil>
      proxy_kept_test.go:294: to chatgpt.com: <nil>
  gofmt -l internal/agent: nothing
  GOOS=linux, darwin, windows go vet -tags nogui ./internal/agent: exit 0
  full suite, go test -tags nogui -count=1 -timeout 45m ./... (481s):
  62 packages ok, the two tests above with them. 14 reds in 7
  packages, none in a file this change touches, each red on b437e4ad
  without it:
    internal/agent TestInstalledVersion, TestHowInstalled,
    TestOmpDetectedWithPiDir, TestCopilotFolderFromAnIDEPluginIsNotTheCLI
    and TestZedAppPath: proc.Look keeps "not found" for a tool written
    in the tick of its folder's last change (#3141). Alone on b437e4ad,
    -count=10: TestHowInstalled red 3 times, TestZedAppPath 7.
    internal/agent TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: their fixture build under
    /tmp meets a /tmp/.git that isn't a repository of this user's
    ("error obtaining VCS status: exit status 128").
    internal/claudebridge TestCallbackGoesPastTheAgentsProxy,
    internal/gateway TestClaudeToolCallbackOnANonLoopbackAddress,
    internal/gui TestGatewayHeldByAContainer and
    TestGatewayFixQuitsOnlyAMagpie: the processes this run started had
    their outgoing connections sent through a proxy (graftcp).
    internal/edit TestWriteAtomicReadOnlyHardLink and internal/testenv
    TestProgram: umask 077 here; the fixtures' own modes go through it
    (0444 is written as 0400).
    internal/netproxy TestBunProxyLoginWithReservedCharacters: Bun
    1.3.11 on PATH names port 80 in the request line (#3397).

Not changed:
  - The memo. In the app, a request Codex logs inside the tick of
    magpie's last read (a connect and its refusal one after the other)
    can reach Drift up to reachedFor late, which the memo allows for
    already. A settle window like filememo's or codex_list_tag's would
    see it at once, at the cost of a read per ask just after each of
    Codex's writes, the reads #1665 took out.
  - The fixtures' journal mode. A WAL fixture would pass these tests
    only while its -wal grows, which stops once it wraps.

Landed by yetone in a batch of Coffemon's patches from the issues.
What was run, go1.26.8, on origin/main cc81ad4ea, then again at
70a0c387c (gofmt, the three vets, the changed tests, umask 077):
- mote (macOS 27): gofmt -l clean; go vet, GOOS=linux go vet -tags
  nogui and GOOS=windows go vet ./...: ok. edit and testenv at umask
  077; agent, provider, library and gateway with an empty .git above
  TMPDIR; davsync, imagemcp, plugin, library, edit, testenv whole; the
  changed tests in agent, provider, gateway and gui; the timing tests
  -count=10 (gateway, davsync, imagemcp) and -count=5 (agent): all ok.
  TestCoreScriptBudget ok: core 1,536,751 of 1,537,034, app.js
  1,312,975 of 1,315,316, no budget raised.
- Red on the old code: on origin/main, TestWriteAtomicReadOnlyHardLink
  at umask 077 and two Reasonix tests under an empty .git; on Linux
  (wsl-run) TestProgram at umask 077 with the old program.go. With
  each fix's code taken back to main's, the Gemini imageConfig tests,
  TestPluginToldWhereTheAgentWorks, TestSkillRepoTraced,
  TestTaggedHooksAlone, TestMainURL/TestMainView and update-auto,
  plugin-github-hooks, plugins-more-discover, plugin-namesake were
  red. Broken by hand: claudeKeyed always true (both
  OwnEndpointNeverAsked tests red), claudeAsk a no-op
  (TestClaudeFirstWriteAsksTheProxy red), GitAt taking any .git folder
  (the three TestGitOnDisk tests red).
- GUI: 123 files (plugins*, update-*, panel/tray/start/context, i18n,
  gui-zh-tw/ja/de and every file that evals app.js parts) in WebKit on
  Linux (wsl-run). 11 files had reds (click-scroll, panel-fold,
  plugin-card-update, plugin-github, plugin-move*, segs-glide,
  tray-totals, update-*), and run again side by side they fail the
  same on origin/main d80d75f30 (79 pass, 26 fail here; 78 and 27
  there). They are click-scroll guards ("the click must not scroll
  the page"), varying by language from run to run (#4385).
  copilot-ghe's route-after-close red is #1949, seen on main 244d80e23
  too. In Chromium on mote, Chromium segfaulted partway through files
  (#4125): context-map, context-scroll-end and context-tune fail the
  same on origin/main 0911a65e7, so the batch's own files
  (plugin-namesake, update-auto, plugins-more-discover,
  plugin-github-hooks) ran in Chromium on Linux: 62 of 62.
- Full suite (go test -tags nogui -count=1 -timeout 45m ./..., mote)
  at 472a0608f (on 70a0c387c): every package ok but internal/gateway,
  whose five reds were #4362's (Antigravity turned-away and
  TestCodexSpentGrokFailingIsTold), fixed on main in d80d75f30. After
  the rebase on 244d80e23 the five pass, and gofmt, the three vets
  and the targeted runs above were run again: ok. In Linux the
  sign-in, copilot, plugin-github*, plugins-more-discover,
  plugin-namesake, plugin-gateway, update-auto, quota-refresh-order
  and i18n files ran in both engines: 475 pass, 4 fail (copilot-ghe
  #1949, plugin-github's click scroll, both red on main too).
- Rebased again on c7171a256, over its budget fix e94bf7a08: gofmt, vet of
  gui, gateway, provider and plugin, TestCoreScriptBudget (core
  1,536,265 of 1,536,586 with the batch's last commit, statusTErr and settingsErr), the Gemini,
  GitOnDisk and plugin tests, and in Linux both engines i18n-keys,
  update-mirror, plugin-namesake, plugins-more-discover,
  plugin-github-hooks and update-auto (138 of 138): ok.
Not tried: the whole GUI suite (mote's WebKit wouldn't launch and its
Chromium segfaulted, #4125); macOS WebKit; the real app's window.
2026-10-30 15:53:22 +08:00

81 lines
2.9 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)
}
}
// forgetReached drops what codexReached kept of Codex's log, for a test of
// what Drift says about each request it logs, asked right after it. Two
// inserts inside one tick of the file system's clock can leave the log's
// size and time as they were (a rollback journal writes the same page
// again; a -wal that wrapped, as Codex's own has, writes over its old
// frames), and the kept read then stands for up to reachedFor, as it does
// in the app. When the log is read again is
// TestCodexReachedReadsLogOnlyWhenItChanged's to test.
func forgetReached() {
reachedMu.Lock()
clear(reachedMemo)
reachedMu.Unlock()
}