From 728cb2d29cc7bee7d237fbbf31f06878f9b1b57e Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 10:55:24 +0800 Subject: [PATCH] otel tests: a request's cached discovery miss outlasts the request on a loaded machine, not only for 50ms (#1811) TestOTelFastRequestTranscriptDiscovery/true wrote the Pi transcript inside the upstream call and then checked that the cached lookup still missed it. Two things could find the transcript first, both wall time: - the exporter's transcript watcher, which fastOTel ticks every 50ms. Its poll refreshes the session index and marks the session seen, so a pause of more than ~50ms between the write and the check turned the test red. This is the failure seen under several concurrent suites. - the session index's 2s discovery cache window. While the watcher ticks it keeps the snapshot fresh, so this showed only once the watcher was held. sessions.TraceIdentityTTL now names the 2s window (behaviour unchanged). The test holds both for the request: OTelSessionsEvery and the window are an hour, so only SessionFresh (the code under test) can find the transcript, however slow the machine is. Siblings with the same assumption: - otlp TestOTelSessionDiscoveredDuringFirstRequest: same cached-miss check after writing the transcript; same hold. - sessions TestTraceSessionIndexSharesMissesAndDoesNotQueueRequests: 100 lookups had to finish inside the 2s window; the window is held for that loop, and the "not queued" wait is 30s instead of 1s (a queued lookup blocks until Unlock, so the bound only delays a real failure). - gateway: the wait-until deadlines for transcript readiness and the Claude child generations go from 6s/8s to 30s; they return as soon as the condition holds. Broken by hand (injected pauses, then removed): - old gateway test, 100ms pause after the write: red ("request must still have a cached discovery miss"); 2100ms: red. New: green at 0, 100ms, 2100ms after the write and 2100ms before it. - new gateway test with only the watcher held, 2100ms before the write: red, so the window hold is needed too. - new gateway test with SessionFresh calling Visible instead of VisibleFresh: red ("gateway spans=2 want=0"), so it still guards the fresh lookup. - old otlp sibling, 100ms and 2100ms pause: red; new: green at 0, 100ms, 2100ms. - old sessions sibling with 25ms per lookup (2.5s): red ("each unknown session repeated discovery"); new: green. Run: - go test -race -count=20 -cpu 1,4 -timeout 60m -run OTel ./internal/gateway: ok (1515s) - go test -race -count=20 -cpu 1,4 -run 'OTel|TraceSession' ./internal/otlp ./internal/sessions: ok - gofmt -l: clean; go vet ./... (darwin); CGO_ENABLED=0 GOOS=windows|linux go vet -tags nogui ./...: clean - Full suite: SUITE_LINE --- internal/gateway/otel_test.go | 13 ++++++++++--- internal/otlp/request_test.go | 7 +++++++ internal/sessions/traces_identity.go | 9 +++++++-- internal/sessions/traces_identity_test.go | 8 +++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/internal/gateway/otel_test.go b/internal/gateway/otel_test.go index 301dd38ec..8049ba314 100644 --- a/internal/gateway/otel_test.go +++ b/internal/gateway/otel_test.go @@ -133,7 +133,7 @@ func TestOTelSessionDedupOnlyUnkeyedLoopback(t *testing.T) { t.Fatal(err) } h.Close() - deadline := time.Now().Add(6 * time.Second) + deadline := time.Now().Add(30 * time.Second) for !otelX.SessionAgent("pi") && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } @@ -260,6 +260,13 @@ func TestOTelFastRequestTranscriptDiscovery(t *testing.T) { t.Setenv("MAGPIE_OTEL_SESSIONS", "true") t.Setenv("MAGPIE_OTEL_METRICS", "false") fastOTel(t) + // The request's cached miss must outlast the request however slow + // the machine is (#1811): neither the transcript watcher's poll nor + // the end of the discovery cache window may read the transcript + // before the export decides. Only SessionFresh may find it. + sessionsEvery, ttl := otlp.OTelSessionsEvery, sessions.TraceIdentityTTL + otlp.OTelSessionsEvery, sessions.TraceIdentityTTL = time.Hour, time.Hour + t.Cleanup(func() { otlp.OTelSessionsEvery, sessions.TraceIdentityTTL = sessionsEvery, ttl }) stop := startOTel() t.Cleanup(stop) req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"m1","messages":[{"role":"user","content":"hello"}]}`)) @@ -809,7 +816,7 @@ func TestOTelClaudeMainAndChildDedupRetainsHelper(t *testing.T) { t.Fatal(err) } } - deadline := time.Now().Add(6 * time.Second) + deadline := time.Now().Add(30 * time.Second) for !otelX.Session(agent, "parent") && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } @@ -820,7 +827,7 @@ func TestOTelClaudeMainAndChildDedupRetainsHelper(t *testing.T) { // alone only proves the user/root event was observed, not final coverage. var batches [][]byte generations := 0 - timer := time.NewTimer(8 * time.Second) + timer := time.NewTimer(30 * time.Second) for generations < 2 { select { case batch := <-received: diff --git a/internal/otlp/request_test.go b/internal/otlp/request_test.go index 489b33936..c5cc3d94a 100644 --- a/internal/otlp/request_test.go +++ b/internal/otlp/request_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/yetone/magpie/internal/sessions" "github.com/yetone/magpie/internal/usage" ) @@ -20,6 +21,12 @@ func TestOTelSessionDiscoveredDuringFirstRequest(t *testing.T) { t.Setenv("MAGPIE_OTEL_HEADERS", "") t.Setenv("MAGPIE_OTEL_SESSIONS", "true") fastOTel(t) + // Begin's cached miss must outlast the test however slow the machine + // is (#1811): neither the transcript watcher's poll nor the end of the + // discovery cache window may find the transcript before skip() does. + sessionsEvery, ttl := OTelSessionsEvery, sessions.TraceIdentityTTL + OTelSessionsEvery, sessions.TraceIdentityTTL = time.Hour, time.Hour + t.Cleanup(func() { OTelSessionsEvery, sessions.TraceIdentityTTL = sessionsEvery, ttl }) stop := StartOTel() t.Cleanup(stop) // a local pi turn with no key, as the gateway describes it diff --git a/internal/sessions/traces_identity.go b/internal/sessions/traces_identity.go index 8c15f9003..d3cbaa0e2 100644 --- a/internal/sessions/traces_identity.go +++ b/internal/sessions/traces_identity.go @@ -43,16 +43,21 @@ type traceHeaderCache struct { ok bool } +// TraceIdentityTTL is how long Visible reuses a discovery scan, a miss +// included, before scanning the transcripts again. Tests that need a +// cached miss to stay cached for a whole request lengthen it. +var TraceIdentityTTL = 2 * time.Second + func (x *TraceSessionIndex) Visible(agent, id string) bool { if agent != "codex" && agent != "pi" { return false } snapshot := x.snapshot.Load() - if snapshot == nil || time.Since(snapshot.at) >= 2*time.Second { + if snapshot == nil || time.Since(snapshot.at) >= TraceIdentityTTL { if x.refresh.TryLock() { // A refresh may have completed while this caller was acquiring the lock. snapshot = x.snapshot.Load() - if snapshot == nil || time.Since(snapshot.at) >= 2*time.Second { + if snapshot == nil || time.Since(snapshot.at) >= TraceIdentityTTL { snapshot = x.update(traceRecentFiles(traceLineFiles())) } x.refresh.Unlock() diff --git a/internal/sessions/traces_identity_test.go b/internal/sessions/traces_identity_test.go index f247017ea..034887553 100644 --- a/internal/sessions/traces_identity_test.go +++ b/internal/sessions/traces_identity_test.go @@ -100,11 +100,17 @@ func TestTraceSessionVisibilityRespectsReaderWindow(t *testing.T) { func TestTraceSessionIndexSharesMissesAndDoesNotQueueRequests(t *testing.T) { x := new(TraceSessionIndex) empty := x.update(nil) + // The 100 lookups share one miss however long they take on a slow + // machine (#1811); the cache window is not what this checks. + ttl := TraceIdentityTTL + t.Cleanup(func() { TraceIdentityTTL = ttl }) + TraceIdentityTTL = time.Hour for i := 0; i < 100; i++ { if x.Visible("codex", fmt.Sprintf("remote-%d", i)) { t.Fatal("unknown session visible") } } + TraceIdentityTTL = ttl if x.snapshot.Load() != empty { t.Fatal("each unknown session repeated discovery") } @@ -118,7 +124,7 @@ func TestTraceSessionIndexSharesMissesAndDoesNotQueueRequests(t *testing.T) { if !visible { t.Fatal("concurrent refresh discarded existing readiness") } - case <-time.After(time.Second): + case <-time.After(30 * time.Second): x.refresh.Unlock() <-done t.Fatal("request queued behind directory discovery")