From 7d19f312940d9099da52ee7c4dca0904203dc68a Mon Sep 17 00:00:00 2001 From: HosniBelfeki Date: Thu, 26 Nov 2026 20:58:47 +0100 Subject: [PATCH] node: do not retry a provider that does not attest the required labels When a provider's credential did not attest a label the caller required, or one of the egress floor, connectMCPSessionWithRetry treated the refusal as a failure to reach the peer. It ran the handshake three times, waited 1s and 2s between attempts, and returned "failed after 3 retries". The provider presents the same credential on every attempt, so the answer cannot change. checkProvider now wraps a failed label check in ErrLabelsNotSatisfied, and the retry loop stops on it as it does on ErrProviderNotGranted. An authorizer that stopped at a run limit is not wrapped, so a timeout is still retried. In TestCallMCPTool_LabelEnforcement and TestCallMCPTool_EgressFloorEnforcement the refused call took about 3.4s and now takes about 0.4s. --- internal/node/labels_gate.go | 8 ++++++++ internal/node/mcp.go | 5 +++-- internal/node/mcp_handlers_test.go | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/node/labels_gate.go b/internal/node/labels_gate.go index cafc1a4a4..1cfd81457 100644 --- a/internal/node/labels_gate.go +++ b/internal/node/labels_gate.go @@ -105,6 +105,11 @@ var ErrProviderUnreachable = errors.New("provider unreachable") // not who the policy says serves that name. var ErrProviderNotGranted = errors.New("provider not granted the service") +// ErrLabelsNotSatisfied marks a provider whose credential does not attest +// every label the caller requires or the egress floor sets: it is enrolled +// and reachable, and its attested labels are not the ones asked for. +var ErrLabelsNotSatisfied = errors.New("provider labels not satisfied") + // ErrHandshakeRateLimited marks a provider that refused the credential // handshake at its per-peer handshake rate limit: it is up, and answered // by resetting the stream with network.StreamRateLimited. A caller that @@ -337,6 +342,9 @@ func (n *AgentMeshNode) checkProvider(providerBiscuit []byte, peerID peer.ID, re } authorizer.AddPolicy(api.AllowIfTruePolicy) if err := authorizer.Authorize(); err != nil { + if stopped, _ := authorizerLimit(err); !stopped { + err = fmt.Errorf("%w: %w", ErrLabelsNotSatisfied, err) + } if len(floor) > 0 { return fmt.Errorf("provider %s does not attest every label of the caller requirement %v and the egress floor %v: %w", peerID, required, floor, err) } diff --git a/internal/node/mcp.go b/internal/node/mcp.go index b8e0ea5f2..44250fc2b 100644 --- a/internal/node/mcp.go +++ b/internal/node/mcp.go @@ -254,8 +254,9 @@ func (n *AgentMeshNode) connectMCPSessionWithRetry(ctx context.Context, targetPe // A denial is an answer, not a failure to reach anyone. Retrying it // just delays the same reply and knocks on the remote peer three times // for one call. That holds for the remote peer refusing the caller - // and for the caller refusing a provider not granted the service. - if errors.Is(err, ErrAuthRejected) || errors.Is(err, ErrProviderNotGranted) { + // and for the caller refusing a provider not granted the service or + // not attesting the labels the call requires. + if errors.Is(err, ErrAuthRejected) || errors.Is(err, ErrProviderNotGranted) || errors.Is(err, ErrLabelsNotSatisfied) { logger.Warnf("[MCP] Connecting to %s denied, not retrying: %v", targetPeer, err) return nil, nil, err } diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.go index 015ea9ca2..d0dd718d2 100644 --- a/internal/node/mcp_handlers_test.go +++ b/internal/node/mcp_handlers_test.go @@ -612,9 +612,15 @@ func TestCallMCPTool_LabelEnforcement(t *testing.T) { t.Fatalf("CallMCPTool with satisfied label requirement should succeed: %v", err) } - // A required label that nodeB's attestation does not satisfy: fail closed. - if _, err := nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, map[string]string{"region": "na-us"}); err == nil { - t.Fatal("CallMCPTool with a non-matching label requirement must fail") + // A required label that nodeB's attestation does not satisfy: fail closed, + // once. The refusal is a verdict on nodeB's credential, and a retry + // presents the same credential again. + _, err = nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, map[string]string{"region": "na-us"}) + if !errors.Is(err, ErrLabelsNotSatisfied) { + t.Fatalf("CallMCPTool with a non-matching label requirement = %v, want ErrLabelsNotSatisfied", err) + } + if strings.Contains(err.Error(), "retries") { + t.Fatalf("CallMCPTool with a non-matching label requirement = %v, want one refusal with no retry", err) } } @@ -882,8 +888,12 @@ func TestCallMCPTool_EgressFloorEnforcement(t *testing.T) { // Floor nodeB does not attest; the caller staying silent must not waive it. nodeA.nodeConfig = &NodeConfigComplete{EgressRequireLabels: map[string]string{"jurisdiction": "eu"}} - if _, err := nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, nil); err == nil { - t.Fatal("CallMCPTool outside the floor with no caller requirement must fail") + _, err = nodeA.CallMCPTool(ctx, nodeB.Host.ID(), "mcp://code-reviewer/review_pr", map[string]any{}, nil) + if !errors.Is(err, ErrLabelsNotSatisfied) { + t.Fatalf("CallMCPTool outside the floor with no caller requirement = %v, want ErrLabelsNotSatisfied", err) + } + if strings.Contains(err.Error(), "retries") { + t.Fatalf("CallMCPTool outside the floor = %v, want one refusal with no retry", err) } }