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")