Skip to content

Agent Mesh

fix(discovery): scope gossip verification timeout per request

Rhein1 / Agoragentic6ecd504dcff7
diff --git a/internal/node/mcp_handlers.go b/internal/node/mcp_handlers.go
index c1380220..1ceb0399 100644
--- a/internal/node/mcp_handlers.go
+++ b/internal/node/mcp_handlers.go
@@ -424,15 +424,23 @@ func gossipToolRows(provs []samdiscovery.Provider, toolName, serviceNameFilter s
// authorization evidence. Each candidate is confirmed through the existing
// authenticated MCP session and exact tools/list response before it is exposed.
func (n *SamNode) verifyGossipToolRows(ctx context.Context, candidates []remoteToolRow) []remoteToolRow {
+ return verifyGossipToolRowsWithFetcher(ctx, candidates, n.fetchRemoteToolDescription)
+}
+
+const (
+ gossipVerificationMaxConcurrent = 8
+ gossipVerificationTimeout = 5 * time.Second
+)
+
+func verifyGossipToolRowsWithFetcher(
+ ctx context.Context,
+ candidates []remoteToolRow,
+ fetchDescription func(context.Context, peer.ID, string) (*remoteToolDescription, error),
+) []remoteToolRow {
if len(candidates) == 0 {
return nil
}
- const maxConcurrent = 8
-
- verifyCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
- defer cancel()
-
type verificationResult struct {
row remoteToolRow
ok bool
@@ -440,8 +448,8 @@ func (n *SamNode) verifyGossipToolRows(ctx context.Context, candidates []remoteT
results := make([]verificationResult, len(candidates))
jobs := make(chan int)
workerCount := len(candidates)
- if workerCount > maxConcurrent {
- workerCount = maxConcurrent
+ if workerCount > gossipVerificationMaxConcurrent {
+ workerCount = gossipVerificationMaxConcurrent
}
var wg sync.WaitGroup
@@ -451,6 +459,8 @@ func (n *SamNode) verifyGossipToolRows(ctx context.Context, candidates []remoteT
defer wg.Done()
for {
select {
+ case <-ctx.Done():
+ return
case index, ok := <-jobs:
if !ok {
return
@@ -461,7 +471,9 @@ func (n *SamNode) verifyGossipToolRows(ctx context.Context, candidates []remoteT
logger.Debugf("[find_remote_tools] invalid gossip peer %q skipped: %v", candidate.PeerID, err)
continue
}
- description, err := n.fetchRemoteToolDescription(verifyCtx, pid, candidate.ToolName)
+ requestCtx, cancel := context.WithTimeout(ctx, gossipVerificationTimeout)
+ description, err := fetchDescription(requestCtx, pid, candidate.ToolName)
+ cancel()
if err != nil {
logger.Debugf("[find_remote_tools] unverified gossip candidate %s on %s skipped: %v", candidate.ToolName, pid, err)
continue
@@ -475,8 +487,6 @@ func (n *SamNode) verifyGossipToolRows(ctx context.Context, candidates []remoteT
Labels: candidate.Labels,
},
}
- case <-verifyCtx.Done():
- return
}
}
}()
@@ -486,7 +496,7 @@ sendCandidates:
for index := range candidates {
select {
case jobs <- index:
- case <-verifyCtx.Done():
+ case <-ctx.Done():
break sendCandidates
}
}
diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.go
index 4c07ee18..9b345725 100644
--- a/internal/node/mcp_handlers_test.go
+++ b/internal/node/mcp_handlers_test.go
@@ -6,11 +6,13 @@ import (
"crypto/ed25519"
"crypto/rand"
"encoding/json"
+ "errors"
+ "fmt"
"io"
-
"net/http"
"net/http/httptest"
"strings"
+ "sync/atomic"
"testing"
"time"
@@ -580,6 +582,82 @@ func TestVerifyGossipToolRows_AuthenticatesEachServiceOnSamePeer(t *testing.T) {
}
}
+func TestVerifyGossipToolRows_UsesIndependentRequestTimeouts(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ _, publicKey, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ peerID, err := peer.IDFromPublicKey(publicKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ candidates := make([]remoteToolRow, 0, gossipVerificationMaxConcurrent+1)
+ for i := range gossipVerificationMaxConcurrent {
+ candidates = append(candidates, remoteToolRow{
+ PeerID: peerID.String(),
+ ToolName: fmt.Sprintf("mcp://slow-%d/review_pr", i),
+ })
+ }
+ candidates = append(candidates, remoteToolRow{
+ PeerID: peerID.String(),
+ ToolName: "mcp://fast/review_pr",
+ })
+
+ allSlowRequestsStarted := make(chan struct{})
+ releaseSlowRequests := make(chan struct{})
+ var slowRequestsStarted atomic.Int32
+ go func() {
+ select {
+ case <-allSlowRequestsStarted:
+ case <-ctx.Done():
+ return
+ }
+ select {
+ case <-time.After(500 * time.Millisecond):
+ close(releaseSlowRequests)
+ case <-ctx.Done():
+ }
+ }()
+
+ var fastRequestBudget time.Duration
+ fetchDescription := func(ctx context.Context, pid peer.ID, toolName string) (*remoteToolDescription, error) {
+ if toolName == "mcp://fast/review_pr" {
+ deadline, ok := ctx.Deadline()
+ if !ok {
+ return nil, errors.New("fast request has no deadline")
+ }
+ fastRequestBudget = time.Until(deadline)
+ return &remoteToolDescription{
+ PeerID: pid.String(),
+ ToolName: toolName,
+ Description: "verified after delayed candidates",
+ }, nil
+ }
+
+ if slowRequestsStarted.Add(1) == gossipVerificationMaxConcurrent {
+ close(allSlowRequestsStarted)
+ }
+ select {
+ case <-releaseSlowRequests:
+ return nil, errors.New("delayed candidate rejected")
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ }
+ }
+
+ rows := verifyGossipToolRowsWithFetcher(ctx, candidates, fetchDescription)
+ if len(rows) != 1 || rows[0].ToolName != "mcp://fast/review_pr" {
+ t.Fatalf("expected only the final candidate to verify, got %+v", rows)
+ }
+ if minimumBudget := gossipVerificationTimeout - 250*time.Millisecond; fastRequestBudget < minimumBudget {
+ t.Fatalf("final candidate inherited an exhausted timeout: got %v, want at least %v", fastRequestBudget, minimumBudget)
+ }
+}
+
func TestCallMCPTool_LabelEnforcement(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()