From ce8fc1afe8f893f8adcbee100ccd5eb49c97bec0 Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Wed, 11 Nov 2026 20:05:47 +0200 Subject: [PATCH 1/3] test(server): wait out the startup orphan-index cleanup before seeding the search gate test backgroundToolIndexing, spawned right after PhaseReady, begins with cleanupOrphanedIndexEntries, which deletes the index documents of every indexed server the upstream manager does not know. TestSearchTools_WithholdsApprovalLockedHits seeds hand-made servers into the index after PhaseReady, so on a loaded runner the cleanup removed orchid:read_item mid-seeding and the approved hit went missing from the 'gadget' query (about 1 in 40 runs at GOMAXPROCS=1 -race). The fixture now also waits for the pass's completion log line. --- .../server/consistency_crosssurface_test.go | 32 ++++++++++++++++++- .../server/search_tools_approval_gate_test.go | 12 ++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/internal/server/consistency_crosssurface_test.go b/internal/server/consistency_crosssurface_test.go index 5253c4be2..b8398d62a 100644 --- a/internal/server/consistency_crosssurface_test.go +++ b/internal/server/consistency_crosssurface_test.go @@ -12,10 +12,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" "github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts" "github.com/smart-mcp-proxy/mcpproxy-go/internal/httpapi" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime" ) // Spec 070 keystone regression (T021 / CN-004 / FR-010 / SC-004). @@ -125,17 +128,44 @@ const consistencyAPIKey = "t021-consistency-key" // a known API key so the REST surface can authenticate. The storage handle is // closed on cleanup so the temp-dir removal succeeds on Windows. func newConsistencyServer(t *testing.T) *Server { + t.Helper() + return newConsistencyServerWithLogger(t, zap.NewNop()) +} + +func newConsistencyServerWithLogger(t *testing.T, logger *zap.Logger) *Server { t.Helper() cfg := config.DefaultConfig() cfg.DataDir = t.TempDir() cfg.Listen = "127.0.0.1:0" cfg.APIKey = consistencyAPIKey - srv, err := NewServer(cfg, zap.NewNop()) + srv, err := NewServer(cfg, logger) require.NoError(t, err) t.Cleanup(func() { _ = srv.Shutdown() }) return srv } +// newSettledConsistencyServer is newConsistencyServer for a test that seeds +// the index and storage by hand: it returns only once the runtime's startup +// work that would otherwise race the seeding has finished. +// +// PhaseReady alone is not enough. StartBackgroundInitialization sets it and then +// spawns backgroundToolIndexing, whose first act is cleanupOrphanedIndexEntries: +// it lists the indexed servers and deletes every one the upstream manager does +// not know. A fixture that indexes a hand-made server between that list and +// the delete (or between two deletes) loses documents at random, which a +// loaded runner makes likely. Waiting for the pass's completion line pins it +// before the seeding starts; the discovery sweep that follows it waits 2s. +func newSettledConsistencyServer(t *testing.T) *Server { + t.Helper() + core, logs := observer.New(zapcore.DebugLevel) + srv := newConsistencyServerWithLogger(t, zap.New(core)) + require.Eventually(t, func() bool { + return srv.runtime.CurrentPhase() == runtime.PhaseReady && + logs.FilterMessage("Orphaned index cleanup completed").Len() > 0 + }, 10*time.Second, 10*time.Millisecond, "fixture: runtime never finished its startup orphan-index cleanup") + return srv +} + // persistedServer reads the actually-persisted ServerConfig back from the // server's live config snapshot (not the function return value), so the // comparison reflects what reached storage. diff --git a/internal/server/search_tools_approval_gate_test.go b/internal/server/search_tools_approval_gate_test.go index 5db033fae..c83729e74 100644 --- a/internal/server/search_tools_approval_gate_test.go +++ b/internal/server/search_tools_approval_gate_test.go @@ -2,12 +2,10 @@ package server import ( "testing" - "time" "github.com/stretchr/testify/require" "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" - "github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime" "github.com/smart-mcp-proxy/mcpproxy-go/internal/storage" ) @@ -18,13 +16,9 @@ import ( // must not be returned — by its canonical "server:tool" id, which the index // resolves to that one document, or by any word that matches it. func TestSearchTools_WithholdsApprovalLockedHits(t *testing.T) { - srv := newConsistencyServer(t) - - // See TestSearchTools_AnnotationsSurviveToTier: seeding before the - // background config sync finishes races it. - require.Eventually(t, func() bool { - return srv.runtime.CurrentPhase() == runtime.PhaseReady - }, 10*time.Second, 10*time.Millisecond, "fixture: runtime never reached PhaseReady") + // Seeding before the runtime's startup sync and orphan-index cleanup finish + // races them (see newSettledConsistencyServer). + srv := newSettledConsistencyServer(t) store := srv.runtime.StorageManager() require.NoError(t, store.SaveUpstreamServer(&config.ServerConfig{Name: "orchid", Enabled: true})) From 1abb02eef39133b33e885ca99e7b397cabe0a234 Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Wed, 11 Nov 2026 21:11:08 +0200 Subject: [PATCH 2/3] test(server): wait for the activating refresh cycle to be counted before asserting telemetry BundleStatus().Source flips to fetch inside the cycle's fetch leg, but the cycle reports its outcome to the telemetry observer only afterwards (recordCycle, logOutcome, notifyRefreshObserver). TestTPAFetchLifecycle read TPABundleRefreshActivations as soon as the source flipped, so on a slow runner it saw 0 instead of 1. Poll for the counter, then assert it is exactly 1. --- internal/server/tpa_bundle_fetch_e2e_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/server/tpa_bundle_fetch_e2e_test.go b/internal/server/tpa_bundle_fetch_e2e_test.go index 6fe90b992..49e0bd40a 100644 --- a/internal/server/tpa_bundle_fetch_e2e_test.go +++ b/internal/server/tpa_bundle_fetch_e2e_test.go @@ -404,6 +404,12 @@ func TestTPAFetchLifecycle_ActivatesReprotectsAndRefusesBadReleases(t *testing.T require.NotNil(t, info.LastActivatedAt) assert.Equal(t, scanner.RefreshTriggerTimer, info.Refresh.LastTrigger, "the scheduler, not a manual refresh, fetched it") require.NotNil(t, f.rt.TelemetryRegistry()) + // The source flips inside the cycle's fetch leg; the cycle reports its + // outcome to the telemetry observer only after that leg returns (recordCycle, + // logOutcome, then notifyRefreshObserver), so the counter lags the status. + e2eEventually(t, "the activating cycle is counted", func() bool { + return f.rt.TelemetryRegistry().Snapshot().TPABundleRefreshActivations >= 1 + }) assert.EqualValues(t, 1, f.rt.TelemetryRegistry().Snapshot().TPABundleRefreshActivations, "exactly one cycle reported activated") // Later cycles see the asset's sequence at or below the watermark and skip From 875c69ff8857bb5feaf6cce916235ab5effe43af Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Wed, 11 Nov 2026 21:13:43 +0200 Subject: [PATCH 3/3] test(server): wait out the startup orphan-index cleanup in the search tier test --- internal/server/search_tools_tier_test.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/server/search_tools_tier_test.go b/internal/server/search_tools_tier_test.go index c44166ab4..a77c3d4ed 100644 --- a/internal/server/search_tools_tier_test.go +++ b/internal/server/search_tools_tier_test.go @@ -2,14 +2,12 @@ package server import ( "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" "github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts" - "github.com/smart-mcp-proxy/mcpproxy-go/internal/runtime" ) // TestSearchTools_AnnotationsSurviveToTier is a regression test for a Spec @@ -24,8 +22,6 @@ import ( // when found via search — the exact "silently not destructive" outcome the // tier feature exists to prevent. func TestSearchTools_AnnotationsSurviveToTier(t *testing.T) { - srv := newConsistencyServer(t) - // newConsistencyServer's cfg.Servers is empty, so NewServer's async // LoadConfiguredServers (StartBackgroundInitialization) is about to // re-sync storage to match it. Seeding the upstream server config before @@ -34,9 +30,9 @@ func TestSearchTools_AnnotationsSurviveToTier(t *testing.T) { // then finds no server and withholds the tool, making the search return // zero hits (flake reproduced under -race full-package runs). Same // pattern as mcp_group_scope_test.go / server_logs_missing_file_test.go. - require.Eventually(t, func() bool { - return srv.runtime.CurrentPhase() == runtime.PhaseReady - }, 10*time.Second, 10*time.Millisecond, "fixture: runtime never reached PhaseReady") + // The startup orphan-index cleanup races the hand-seeded index documents + // the same way (see newSettledConsistencyServer), so wait for both. + srv := newSettledConsistencyServer(t) require.NoError(t, srv.runtime.StorageManager().SaveUpstreamServer(&config.ServerConfig{ Name: "danger",