Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: a member is a leaf of the gossip
Antonio Ojea978ffcacf93a
diff --git a/internal/node/discovery/announcer.go b/internal/node/discovery/announcer.godeleted file mode 100644index fb1bc1c1..00000000--- a/internal/node/discovery/announcer.go+++ /dev/null@@ -1,187 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package discovery--import (- "context"- "math/rand/v2"- "sync"- "time"-- "github.com/google/agentmesh/api"- pubsub "github.com/libp2p/go-libp2p-pubsub"- "github.com/libp2p/go-libp2p/core/peer"- "google.golang.org/protobuf/proto"- "google.golang.org/protobuf/types/known/timestamppb"-)--// Discovery is the node-facing handle: it announces local services-// (provider role) and maintains an interest-scoped view of remote-// providers (consumer role) over a shared set of gossip topics.-type Discovery struct {- ps *pubsub.PubSub- self peer.ID- tm *topicManager-- interval time.Duration- interestTTL time.Duration- staleAfter time.Duration- maxProviders int-- runCtx context.Context-- // announceTopics is only touched by the announce loop goroutine.- announceTopics map[string]*heldTopic-- viewMu sync.Mutex- interests map[string]*interest- providers map[string]Provider // keyed peer|type|service-}--type heldTopic struct {- topic *pubsub.Topic- release func()-}--type interest struct {- sub *pubsub.Subscription- release func()- expires time.Time-}--// Option tweaks Discovery timing/bounds (used by tests).-type Option func(*Discovery)--func WithIntervals(announce, interestTTL, staleAfter time.Duration) Option {- return func(d *Discovery) {- d.interval = announce- d.interestTTL = interestTTL- d.staleAfter = staleAfter- }-}--func WithMaxProviders(n int) Option {- return func(d *Discovery) { d.maxProviders = n }-}--func New(ps *pubsub.PubSub, self peer.ID, opts ...Option) *Discovery {- d := &Discovery{- ps: ps,- self: self,- tm: newTopicManager(ps),- interval: DefaultAnnounceInterval,- interestTTL: DefaultInterestTTL,- staleAfter: DefaultStaleAfter,- maxProviders: DefaultMaxProviders,- announceTopics: map[string]*heldTopic{},- interests: map[string]*interest{},- providers: map[string]Provider{},- }- for _, o := range opts {- o(d)- }- return d-}--// Start runs the announce and interest-janitor loops until ctx is done.-// src may be nil for consume-only nodes.-func (d *Discovery) Start(ctx context.Context, src SourceFunc) {- d.runCtx = ctx- go d.janitorLoop(ctx)- if src != nil {- go d.announceLoop(ctx, src)- }-}--func (d *Discovery) announceLoop(ctx context.Context, src SourceFunc) {- for {- // ±20% jitter to avoid mesh-wide synchronization.- jittered := d.interval + time.Duration((rand.Float64()-0.5)*0.4*float64(d.interval))- select {- case <-ctx.Done():- for _, held := range d.announceTopics {- held.release()- }- d.announceTopics = map[string]*heldTopic{}- return- case <-time.After(jittered):- d.announceOnce(ctx, src())- }- }-}--// announceOnce reconciles the joined topic set with the desired one and-// publishes on every desired topic that currently has subscribers.-func (d *Discovery) announceOnce(ctx context.Context, anns []Announcement) {- desired := map[string][][]byte{}-- for _, ann := range anns {- msg := &api.ServiceAnnounce{- PeerId: d.self.String(),- Type: ann.Type,- ServiceName: ann.Name,- Keys: ann.Keys,- Labels: ann.Labels,- ActiveRequests: ann.Load.ActiveRequests,- LatencyEwmaMs: ann.Load.LatencyEWMAMs,- AnnounceTime: timestamppb.Now(),- }- if err := api.ValidateServiceAnnounce(msg); err != nil {- logger.Warnf("[Discovery] skipping invalid announcement for %q: %v", ann.Name, err)- continue- }- data, err := proto.Marshal(msg)- if err != nil {- continue- }- for _, key := range ann.Keys {- name, err := api.DiscoveryTopic(ann.Type, key)- if err != nil {- continue- }- desired[name] = append(desired[name], data)- }- }-- // Release topics no longer served.- for name, held := range d.announceTopics {- if _, ok := desired[name]; !ok {- held.release()- delete(d.announceTopics, name)- }- }-- for name, msgs := range desired {- held, ok := d.announceTopics[name]- if !ok {- topic, release, err := d.tm.acquire(name)- if err != nil {- logger.Warnf("[Discovery] join %s: %v", name, err)- continue- }- held = &heldTopic{topic: topic, release: release}- d.announceTopics[name] = held- }- // Publish-when-subscribed: silence costs nothing on an idle mesh.- if len(held.topic.ListPeers()) == 0 {- continue- }- for _, data := range msgs {- if err := held.topic.Publish(ctx, data); err != nil {- logger.Debugf("[Discovery] publish %s: %v", name, err)- }- }- }-}diff --git a/internal/node/discovery/discovery.go b/internal/node/discovery/discovery.godeleted file mode 100644index d2e94cb1..00000000--- a/internal/node/discovery/discovery.go+++ /dev/null@@ -1,91 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--// Package discovery provides interest-scoped service announcements over-// GossipSub. Providers announce routing keys (model IDs, tool names) on-// per-key topics only while those topics have subscribers; consumers-// subscribe only for keys they actively use. Cost therefore scales with-// interest, not mesh size. Announcements are routing hints authenticated by-// the pubsub layer's message signing — never authorization inputs.-package discovery--import (- "time"-- "github.com/google/agentmesh/api"- golog "github.com/ipfs/go-log/v2"-)--var logger = golog.Logger("agentmesh-discovery")--const (- // DefaultAnnounceInterval is how often a provider publishes on topics- // with subscribers; each tick is jittered ±20%.- DefaultAnnounceInterval = 10 * time.Second- // DefaultInterestTTL is how long a consumer stays subscribed to a key's- // topic after the last Ensure call for it.- DefaultInterestTTL = 5 * time.Minute- // DefaultStaleAfter is the age beyond which a provider entry is no- // longer returned by Providers.- DefaultStaleAfter = 30 * time.Second- // DefaultMaxProviders bounds the consumer-side provider table.- DefaultMaxProviders = 1024- // MaxProvidersPerSigner bounds how many entries one peer may hold in- // the table: entries are keyed by signer|type|service, so without this- // a single peer announcing many service names could evict everyone- // else's entries (the global cap evicts oldest, not loudest).- MaxProvidersPerSigner = 16- // maxAnnounceSkew rejects announcements too old or too far in the- // future (replay/clock defense; pubsub dedup handles exact replays).- maxAnnounceSkew = 2 * time.Minute-)--// Load carries a provider's runtime hints. Zero values mean unknown.-type Load struct {- ActiveRequests uint32- LatencyEWMAMs float64-}--// Announcement is one service's advertised state, fed by the node.-type Announcement struct {- Type api.ServiceType- Name string- Keys []string- Labels map[string]string- Load Load-}--// SourceFunc supplies the current local announcements on each tick.-type SourceFunc func() []Announcement--// Provider is a remote service observed via gossip.-type Provider struct {- PeerID string- Type api.ServiceType- Service string- Keys []string- Labels map[string]string- Load Load- LastSeen time.Time-}--// ServesKey reports whether the provider advertised the given routing key.-func (p Provider) ServesKey(key string) bool {- for _, k := range p.Keys {- if k == key {- return true- }- }- return false-}diff --git a/internal/node/discovery/discovery_test.go b/internal/node/discovery/discovery_test.godeleted file mode 100644index 89035e97..00000000--- a/internal/node/discovery/discovery_test.go+++ /dev/null@@ -1,371 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package discovery--import (- "context"- "crypto/rand"- "sync/atomic"- "testing"- "time"-- "github.com/google/agentmesh/api"- pubsub "github.com/libp2p/go-libp2p-pubsub"- pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"- "github.com/libp2p/go-libp2p/core/crypto"- "github.com/libp2p/go-libp2p/core/peer"- mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"- "google.golang.org/protobuf/proto"- "google.golang.org/protobuf/types/known/timestamppb"-)--// newPair builds two connected in-memory hosts with gossipsub and Discovery.-func newPair(t *testing.T) (provider, consumer *Discovery) {- t.Helper()- ctx, cancel := context.WithCancel(context.Background())- t.Cleanup(cancel)-- mn := mocknet.New()- h1, err := mn.GenPeer()- if err != nil {- t.Fatal(err)- }- h2, err := mn.GenPeer()- if err != nil {- t.Fatal(err)- }- if err := mn.LinkAll(); err != nil {- t.Fatal(err)- }- if err := mn.ConnectAllButSelf(); err != nil {- t.Fatal(err)- }-- ps1, err := pubsub.NewGossipSub(ctx, h1)- if err != nil {- t.Fatal(err)- }- ps2, err := pubsub.NewGossipSub(ctx, h2)- if err != nil {- t.Fatal(err)- }-- opts := []Option{WithIntervals(100*time.Millisecond, 5*time.Second, 2*time.Second)}- provider = New(ps1, h1.ID(), opts...)- consumer = New(ps2, h2.ID(), opts...)- return provider, consumer-}--func TestAnnounceReachesInterestedConsumer(t *testing.T) {- provider, consumer := newPair(t)- ctx, cancel := context.WithCancel(context.Background())- t.Cleanup(cancel)-- provider.Start(ctx, func() []Announcement {- return []Announcement{{- Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- Name: "llm",- Keys: []string{"model-a", "model-b"},- Labels: map[string]string{"region": "eu"},- Load: Load{ActiveRequests: 3},- }}- })- consumer.Start(ctx, nil)-- // Interest registered after the announcer is already running: the- // publish-when-subscribed gate must open once the subscription spreads.- consumer.Ensure(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-a")-- deadline := time.Now().Add(10 * time.Second)- for time.Now().Before(deadline) {- if ps := consumer.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-a"); len(ps) == 1 {- p := ps[0]- if p.Service != "llm" || p.Labels["region"] != "eu" || p.Load.ActiveRequests != 3 {- t.Fatalf("unexpected provider: %+v", p)- }- if !p.ServesKey("model-b") {- t.Fatalf("provider should carry all keys, got %v", p.Keys)- }- // Uninterested key on the same service: not subscribed, but the- // payload still resolves it once observed.- if ps2 := consumer.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-b"); len(ps2) != 1 {- t.Fatalf("model-b should resolve from the same announcement, got %v", ps2)- }- // Wrong type must not match.- if ps3 := consumer.Providers(api.ServiceType_SERVICE_TYPE_MCP, "model-a"); len(ps3) != 0 {- t.Fatalf("MCP lookup should be empty, got %v", ps3)- }- return- }- time.Sleep(50 * time.Millisecond)- }- t.Fatal("timeout waiting for announcement to reach consumer")-}--func TestProvidersGoStaleWithoutAnnouncements(t *testing.T) {- provider, consumer := newPair(t)- ctx, cancel := context.WithCancel(context.Background())- t.Cleanup(cancel)-- var announcing atomic.Bool- announcing.Store(true)- provider.Start(ctx, func() []Announcement {- if !announcing.Load() {- return nil- }- return []Announcement{{- Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- Name: "llm",- Keys: []string{"model-a"},- }}- })- consumer.Start(ctx, nil)- consumer.Ensure(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-a")-- deadline := time.Now().Add(10 * time.Second)- for time.Now().Before(deadline) {- if len(consumer.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-a")) == 1 {- break- }- time.Sleep(50 * time.Millisecond)- }-- announcing.Store(false)- deadline = time.Now().Add(10 * time.Second)- for time.Now().Before(deadline) {- if len(consumer.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, "model-a")) == 0 {- return // went stale as expected- }- time.Sleep(100 * time.Millisecond)- }- t.Fatal("provider never went stale after announcements stopped")-}--// testPeerID generates a peer identity for constructing raw messages.-func testPeerID(t *testing.T) peer.ID {- t.Helper()- priv, _, err := crypto.GenerateEd25519Key(rand.Reader)- if err != nil {- t.Fatal(err)- }- pid, err := peer.IDFromPrivateKey(priv)- if err != nil {- t.Fatal(err)- }- return pid-}--func rawMessage(t *testing.T, from peer.ID, ann *api.ServiceAnnounce) *pubsub.Message {- t.Helper()- data, err := proto.Marshal(ann)- if err != nil {- t.Fatal(err)- }- return &pubsub.Message{Message: &pubsub_pb.Message{From: []byte(from), Data: data}}-}--func TestObserveRejectsForgedAndStale(t *testing.T) {- d := New(nil, testPeerID(t))- signer := testPeerID(t)- other := testPeerID(t)-- valid := &api.ServiceAnnounce{- PeerId: signer.String(),- Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm",- Keys: []string{"m1"},- AnnounceTime: timestamppb.Now(),- }-- tests := []struct {- name string- msg *pubsub.Message- want int- }{- {"valid announce accepted", rawMessage(t, signer, valid), 1},- {"peer_id not matching signer dropped", rawMessage(t, other, valid), 0},- {"stale timestamp dropped", rawMessage(t, signer, &api.ServiceAnnounce{- PeerId: signer.String(), Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"},- AnnounceTime: timestamppb.New(time.Now().Add(-10 * time.Minute)),- }), 0},- {"future timestamp dropped", rawMessage(t, signer, &api.ServiceAnnounce{- PeerId: signer.String(), Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"},- AnnounceTime: timestamppb.New(time.Now().Add(10 * time.Minute)),- }), 0},- {"invalid payload dropped", &pubsub.Message{- Message: &pubsub_pb.Message{From: []byte(signer), Data: []byte("junk")},- }, 0},- }- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- d.providers = map[string]Provider{}- d.observe(tt.msg)- if got := len(d.providers); got != tt.want {- t.Errorf("providers: got %d, want %d", got, tt.want)- }- })- }-}--func TestProviderTableIsBounded(t *testing.T) {- d := New(nil, testPeerID(t), WithMaxProviders(2))- for range 3 {- signer := testPeerID(t)- d.observe(rawMessage(t, signer, &api.ServiceAnnounce{- PeerId: signer.String(), Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"},- AnnounceTime: timestamppb.Now(),- }))- }- if got := len(d.providers); got != 2 {- t.Fatalf("provider table size: got %d, want 2", got)- }-}--// One peer announcing many distinct service names must not be able to push-// everyone else out of a full table: the global cap evicts the oldest entry,-// which is whoever announced least recently, not whoever announced most.-func TestProviderTableCapsEntriesPerSigner(t *testing.T) {- d := New(nil, testPeerID(t), WithMaxProviders(MaxProvidersPerSigner+4))- honest := testPeerID(t)- d.observe(rawMessage(t, honest, &api.ServiceAnnounce{- PeerId: honest.String(), Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"},- AnnounceTime: timestamppb.Now(),- }))-- loud := testPeerID(t)- for i := range 3 * MaxProvidersPerSigner {- d.observe(rawMessage(t, loud, &api.ServiceAnnounce{- PeerId: loud.String(), Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "svc-" + string(rune('a'+i%26)) + string(rune('a'+i/26)), Keys: []string{"m1"},- AnnounceTime: timestamppb.Now(),- }))- }-- if got := d.countBySignerLocked(loud.String()); got != MaxProvidersPerSigner {- t.Errorf("loud signer holds %d entries, want exactly %d", got, MaxProvidersPerSigner)- }- if got := len(d.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, "m1")); got != MaxProvidersPerSigner+1 {- t.Errorf("providers for m1: got %d, want %d", got, MaxProvidersPerSigner+1)- }- if d.countBySignerLocked(honest.String()) != 1 {- t.Error("the honest signer's entry was evicted by another peer's announcements")- }-}--func TestPeerLabels(t *testing.T) {- d := New(nil, testPeerID(t))- signer := testPeerID(t)- d.observe(rawMessage(t, signer, &api.ServiceAnnounce{- PeerId: signer.String(), Type: api.ServiceType_SERVICE_TYPE_MCP,- ServiceName: "reviewer", Keys: []string{"review_pr"},- Labels: map[string]string{"region": "eu"},- AnnounceTime: timestamppb.Now(),- }))-- if got := d.PeerLabels(signer.String()); got["region"] != "eu" {- t.Errorf("PeerLabels: got %v, want region=eu", got)- }- if got := d.PeerLabels("unknown-peer"); got != nil {- t.Errorf("PeerLabels for unknown peer: got %v, want nil", got)- }-}--func TestValidateServiceAnnounceCaps(t *testing.T) {- base := func() *api.ServiceAnnounce {- return &api.ServiceAnnounce{- PeerId: "12D3KooWTest", Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"}, AnnounceTime: timestamppb.Now(),- }- }- tooMany := make([]string, api.MaxAnnounceKeys+1)- for i := range tooMany {- tooMany[i] = "m"- }-- tests := []struct {- name string- mutate func(*api.ServiceAnnounce)- wantErr bool- }{- {"valid", func(a *api.ServiceAnnounce) {}, false},- {"no keys", func(a *api.ServiceAnnounce) { a.Keys = nil }, true},- {"too many keys", func(a *api.ServiceAnnounce) { a.Keys = tooMany }, true},- {"empty peer", func(a *api.ServiceAnnounce) { a.PeerId = "" }, true},- {"unspecified type", func(a *api.ServiceAnnounce) { a.Type = api.ServiceType_SERVICE_TYPE_UNSPECIFIED }, true},- {"no announce time", func(a *api.ServiceAnnounce) { a.AnnounceTime = nil }, true},- {"oversized label", func(a *api.ServiceAnnounce) {- a.Labels = map[string]string{"k": string(make([]byte, api.MaxAnnounceStringLen+1))}- }, true},- {"free-form region label", func(a *api.ServiceAnnounce) {- a.Labels = map[string]string{"region": "us-east-1"}- }, false},- }- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- a := base()- tt.mutate(a)- if err := api.ValidateServiceAnnounce(a); (err != nil) != tt.wantErr {- t.Errorf("ValidateServiceAnnounce: err=%v, wantErr=%v", err, tt.wantErr)- }- })- }-}--func TestObserveKeysOnTheCanonicalPeerID(t *testing.T) {- d := New(nil, testPeerID(t))- signer := testPeerID(t)-- alias := peer.ToCid(signer).String()- if alias == signer.String() {- t.Fatal("peer.ToCid did not produce a second spelling of the signer")- }- spellings := []string{signer.String(), alias}- for _, spelling := range spellings {- decoded, err := peer.Decode(spelling)- if err != nil || decoded != signer {- t.Fatalf("%q does not decode to the signer (err %v)", spelling, err)- }- }-- announce := func(spelling string) {- t.Helper()- d.observe(rawMessage(t, signer, &api.ServiceAnnounce{- PeerId: spelling, Type: api.ServiceType_SERVICE_TYPE_INFERENCE,- ServiceName: "llm", Keys: []string{"m1"},- Labels: map[string]string{"region": "eu"},- AnnounceTime: timestamppb.Now(),- }))- }-- for _, spelling := range spellings {- announce(spelling)- if got := len(d.providers); got != 1 {- t.Fatalf("after announcing as %q: got %d providers, want 1: %+v", spelling, got, d.providers)- }- for key, p := range d.providers {- if p.PeerID != signer.String() {- t.Errorf("provider keyed %q carries peer id %q, want %q", key, p.PeerID, signer.String())- }- }- }-- if got := d.PeerLabels(signer.String()); got["region"] != "eu" {- t.Errorf("PeerLabels under the canonical id: got %v, want region=eu", got)- }-}diff --git a/internal/node/discovery/topics.go b/internal/node/discovery/topics.godeleted file mode 100644index 676cec80..00000000--- a/internal/node/discovery/topics.go+++ /dev/null@@ -1,71 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package discovery--import (- "sync"-- pubsub "github.com/libp2p/go-libp2p-pubsub"-)--// topicManager refcounts pubsub topic handles: gossipsub allows a single-// Join per topic per host, and the announcer and the view may need the-// same topic (a node can provide and consume the same key).-type topicManager struct {- ps *pubsub.PubSub- mu sync.Mutex- topics map[string]*topicRef-}--type topicRef struct {- topic *pubsub.Topic- refs int-}--func newTopicManager(ps *pubsub.PubSub) *topicManager {- return &topicManager{ps: ps, topics: map[string]*topicRef{}}-}--// acquire joins the topic (or reuses the existing handle) and returns a-// release func. The topic is closed when the last holder releases it.-func (tm *topicManager) acquire(name string) (*pubsub.Topic, func(), error) {- tm.mu.Lock()- defer tm.mu.Unlock()- ref, ok := tm.topics[name]- if !ok {- topic, err := tm.ps.Join(name)- if err != nil {- return nil, nil, err- }- ref = &topicRef{topic: topic}- tm.topics[name] = ref- }- ref.refs++- var once sync.Once- release := func() {- once.Do(func() {- tm.mu.Lock()- defer tm.mu.Unlock()- ref.refs--- if ref.refs == 0 {- if err := ref.topic.Close(); err != nil {- logger.Debugf("[Discovery] closing topic %s: %v", name, err)- }- delete(tm.topics, name)- }- })- }- return ref.topic, release, nil-}diff --git a/internal/node/discovery/view.go b/internal/node/discovery/view.godeleted file mode 100644index 1c48e553..00000000--- a/internal/node/discovery/view.go+++ /dev/null@@ -1,220 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package discovery--import (- "context"- "time"-- "github.com/google/agentmesh/api"- pubsub "github.com/libp2p/go-libp2p-pubsub"- "github.com/libp2p/go-libp2p/core/peer"- "google.golang.org/protobuf/proto"-)--// Ensure registers interest in a routing key: it subscribes to the key's-// topic (if not already) and refreshes the interest TTL. Safe to call on-// every request for the key.-func (d *Discovery) Ensure(t api.ServiceType, key string) {- name, err := api.DiscoveryTopic(t, key)- if err != nil {- return- }- d.viewMu.Lock()- defer d.viewMu.Unlock()- if in, ok := d.interests[name]; ok {- in.expires = time.Now().Add(d.interestTTL)- return- }- topic, release, err := d.tm.acquire(name)- if err != nil {- logger.Warnf("[Discovery] join %s: %v", name, err)- return- }- sub, err := topic.Subscribe()- if err != nil {- release()- logger.Warnf("[Discovery] subscribe %s: %v", name, err)- return- }- d.interests[name] = &interest{- sub: sub,- release: release,- expires: time.Now().Add(d.interestTTL),- }- ctx := d.runCtx- if ctx == nil {- ctx = context.Background()- }- go d.readLoop(ctx, sub)-}--// Providers returns the fresh, non-self providers observed for a key.-func (d *Discovery) Providers(t api.ServiceType, key string) []Provider {- d.viewMu.Lock()- defer d.viewMu.Unlock()- cutoff := time.Now().Add(-d.staleAfter)- var out []Provider- for _, p := range d.providers {- if p.Type == t && p.LastSeen.After(cutoff) && p.ServesKey(key) {- out = append(out, p)- }- }- return out-}--// PeerLabels returns the labels last observed for a peer, if any. Labels are-// node-level claims (e.g. region), so any fresh entry of the peer suffices.-func (d *Discovery) PeerLabels(peerID string) map[string]string {- d.viewMu.Lock()- defer d.viewMu.Unlock()- cutoff := time.Now().Add(-d.staleAfter)- for _, p := range d.providers {- if p.PeerID == peerID && p.LastSeen.After(cutoff) && len(p.Labels) > 0 {- return p.Labels- }- }- return nil-}--// readLoop consumes one subscription until it is cancelled or ctx ends.-func (d *Discovery) readLoop(ctx context.Context, sub *pubsub.Subscription) {- for {- msg, err := sub.Next(ctx)- if err != nil {- return // subscription cancelled or ctx done- }- if msg.GetFrom() == d.self {- continue- }- d.observe(msg)- }-}--// observe validates a gossiped announcement and records the provider.-func (d *Discovery) observe(msg *pubsub.Message) {- var ann api.ServiceAnnounce- if err := proto.Unmarshal(msg.Data, &ann); err != nil {- logger.Debugf("[Discovery] dropping undecodable announce from %s: %v", msg.GetFrom(), err)- return- }- if err := api.ValidateServiceAnnounce(&ann); err != nil {- logger.Debugf("[Discovery] dropping invalid announce from %s: %v", msg.GetFrom(), err)- return- }- // The claimed identity must be the pubsub message signer.- claimed, err := peer.Decode(ann.GetPeerId())- if err != nil || claimed != msg.GetFrom() {- logger.Warnf("[Discovery] dropping announce with peer_id %q not matching signer %s", ann.GetPeerId(), msg.GetFrom())- return- }- ts := ann.GetAnnounceTime().AsTime()- if age := time.Since(ts); age > maxAnnounceSkew || age < -maxAnnounceSkew {- logger.Debugf("[Discovery] dropping stale/future announce from %s (age %v)", msg.GetFrom(), time.Since(ts))- return- }-- typeStr, _ := api.ServiceTypeToString(ann.GetType())- signer := claimed.String()- entryKey := signer + "|" + typeStr + "|" + ann.GetServiceName()-- d.viewMu.Lock()- defer d.viewMu.Unlock()- if _, exists := d.providers[entryKey]; !exists {- // The signer's own oldest entry goes first: a peer that announces- // more than its share only ever displaces itself.- if d.countBySignerLocked(signer) >= MaxProvidersPerSigner {- d.evictOldestLocked(signer)- } else if len(d.providers) >= d.maxProviders {- d.evictOldestLocked("")- }- }- d.providers[entryKey] = Provider{- PeerID: signer,- Type: ann.GetType(),- Service: ann.GetServiceName(),- Keys: ann.GetKeys(),- Labels: ann.GetLabels(),- Load: Load{- ActiveRequests: ann.GetActiveRequests(),- LatencyEWMAMs: ann.GetLatencyEwmaMs(),- },- LastSeen: time.Now(),- }-}--func (d *Discovery) countBySignerLocked(signer string) int {- n := 0- for _, p := range d.providers {- if p.PeerID == signer {- n++- }- }- return n-}--// evictOldestLocked removes the least recently seen entry, restricted to-// one signer's entries when signer is non-empty.-func (d *Discovery) evictOldestLocked(signer string) {- var oldestKey string- var oldest time.Time- for k, p := range d.providers {- if signer != "" && p.PeerID != signer {- continue- }- if oldestKey == "" || p.LastSeen.Before(oldest) {- oldestKey, oldest = k, p.LastSeen- }- }- if oldestKey != "" {- delete(d.providers, oldestKey)- }-}--// janitorLoop drops expired interests and stale provider entries.-func (d *Discovery) janitorLoop(ctx context.Context) {- ticker := time.NewTicker(d.interestTTL / 4)- defer ticker.Stop()- for {- select {- case <-ctx.Done():- d.viewMu.Lock()- for name, in := range d.interests {- in.sub.Cancel()- in.release()- delete(d.interests, name)- }- d.viewMu.Unlock()- return- case <-ticker.C:- now := time.Now()- d.viewMu.Lock()- for name, in := range d.interests {- if now.After(in.expires) {- in.sub.Cancel()- in.release()- delete(d.interests, name)- }- }- cutoff := now.Add(-2 * d.staleAfter)- for k, p := range d.providers {- if p.LastSeen.Before(cutoff) {- delete(d.providers, k)- }- }- d.viewMu.Unlock()- }- }-}diff --git a/internal/node/discovery_source.go b/internal/node/discovery_source.godeleted file mode 100644index 9ff9b702..00000000--- a/internal/node/discovery_source.go+++ /dev/null@@ -1,93 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package node--import (- "context"- "time"-- "github.com/google/agentmesh/api"- "github.com/google/agentmesh/internal/node/discovery"-)--// discoverySourceProbeTimeout bounds each backend model probe per tick.-const discoverySourceProbeTimeout = 2 * time.Second--// activeTracker is implemented by services that report in-flight load.-type activeTracker interface {- ActiveRequests() uint32-}--// toolLister is implemented by services that report served MCP tool names.-type toolLister interface {- Tools(ctx context.Context) ([]string, error)-}--// capKeys bounds announced keys to the wire cap; keys are expected sorted so-// truncation is deterministic across ticks.-func capKeys(keys []string) []string {- if len(keys) > api.MaxAnnounceKeys {- return keys[:api.MaxAnnounceKeys]- }- return keys-}--// discoverySource builds gossip announcements from the registered services:-// inference services announce model IDs, MCP services announce tool names.-func (n *AgentMeshNode) discoverySource() []discovery.Announcement {- labels := n.labels()- var out []discovery.Announcement- for _, info := range n.services.List(api.ServiceType_SERVICE_TYPE_UNSPECIFIED) {- svc, ok := n.services.Get(info.GetName())- if !ok {- continue- }- ctx, cancel := context.WithTimeout(context.Background(), discoverySourceProbeTimeout)- keys, err := serviceKeys(ctx, svc, info.GetType())- cancel()- if err != nil || len(keys) == 0 {- continue- }- ann := discovery.Announcement{- Type: info.GetType(),- Name: info.GetName(),- Keys: capKeys(keys),- Labels: labels,- }- if tracker, ok := svc.(activeTracker); ok {- ann.Load.ActiveRequests = tracker.ActiveRequests()- }- out = append(out, ann)- }- return out-}--// serviceKeys returns the routing keys a service serves, per its type.-func serviceKeys(ctx context.Context, svc Service, t api.ServiceType) ([]string, error) {- switch t {- case api.ServiceType_SERVICE_TYPE_INFERENCE:- if lister, ok := svc.(modelLister); ok {- return lister.Models(ctx)- }- case api.ServiceType_SERVICE_TYPE_MCP:- if lister, ok := svc.(toolLister); ok {- return lister.Tools(ctx)- }- case api.ServiceType_SERVICE_TYPE_EGRESS:- // The destination name is the only key: callers look a hostname up.- return []string{svc.Info().GetName()}, nil- }- return nil, nil-}diff --git a/internal/node/mcp_discovery_test.go b/internal/node/mcp_discovery_test.godeleted file mode 100644index 55ce874d..00000000--- a/internal/node/mcp_discovery_test.go+++ /dev/null@@ -1,214 +0,0 @@-// Copyright 2026 Google LLC-//-// Licensed under the Apache License, Version 2.0 (the "License");-// you may not use this file except in compliance with the License.-// You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing, software-// distributed under the License is distributed on an "AS IS" BASIS,-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-// See the License for the specific language governing permissions and-// limitations under the License.--package node--import (- "context"- "fmt"- "net/http/httptest"- "reflect"- "sort"- "testing"-- "github.com/google/agentmesh/api"- samdiscovery "github.com/google/agentmesh/internal/node/discovery"- "github.com/modelcontextprotocol/go-sdk/mcp"-)--func TestMCPService_Tools(t *testing.T) {- backend := httptest.NewServer(newFakeMCPHandler(t, []*mcp.Tool{- {Name: "zeta", Description: "z", InputSchema: map[string]any{"type": "object"}},- {Name: "alpha", Description: "a", InputSchema: map[string]any{"type": "object"}},- }))- defer backend.Close()-- svc := &MCPService{baseService: baseService{- info: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "tools-svc"},- backend: &api.RegisterServiceRequest_TargetUrl{TargetUrl: backend.URL},- }}- if err := svc.Init(context.Background()); err != nil {- t.Fatalf("Init: %v", err)- }-- got, err := svc.Tools(context.Background())- if err != nil {- t.Fatalf("Tools: %v", err)- }- if want := []string{"alpha", "zeta"}; !reflect.DeepEqual(got, want) {- t.Errorf("Tools: got %v, want %v (sorted)", got, want)- }-- // Cached: survives the backend going away.- backend.Close()- got, err = svc.Tools(context.Background())- if err != nil || len(got) != 2 {- t.Errorf("cached Tools: got %v, err %v", got, err)- }-}--// fakeToolService is a local MCP Service reporting served tool names.-type fakeToolService struct {- testService- tools []string- err error-}--func (f *fakeToolService) Tools(_ context.Context) ([]string, error) {- return f.tools, f.err-}--func TestDiscoverySource(t *testing.T) {- node := &AgentMeshNode{- services: NewServiceRegistry(&fakeDHT{}, 0),- nodeConfig: &NodeConfigComplete{Labels: map[string]string{"region": "EU"}},- }- ctx := context.Background()-- llm := newFakeModelService("llm", nil, "model-b", "model-a")- tools := &fakeToolService{- testService: testService{info: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "reviewer"}},- tools: []string{"review_pr", "lint"},- }- broken := &fakeToolService{- testService: testService{info: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "broken"}},- err: fmt.Errorf("backend down"),- }- for _, svc := range []Service{llm, tools, broken} {- if err := node.services.Register(ctx, svc); err != nil {- t.Fatalf("Register: %v", err)- }- }-- anns := node.discoverySource()- if len(anns) != 2 {- t.Fatalf("announcements: got %d (%+v), want 2", len(anns), anns)- }- byName := map[string]samdiscovery.Announcement{}- for _, a := range anns {- byName[a.Name] = a- }- if a := byName["llm"]; a.Type != api.ServiceType_SERVICE_TYPE_INFERENCE || len(a.Keys) != 2 {- t.Errorf("llm announcement: %+v", a)- }- if a := byName["reviewer"]; a.Type != api.ServiceType_SERVICE_TYPE_MCP || len(a.Keys) != 2 {- t.Errorf("reviewer announcement: %+v", a)- }- for name, a := range byName {- if a.Labels["region"] != "EU" {- t.Errorf("%s labels: got %v, want region=EU", name, a.Labels)- }- }-}--func TestCapKeys(t *testing.T) {- keys := make([]string, api.MaxAnnounceKeys+5)- for i := range keys {- keys[i] = fmt.Sprintf("k%03d", i)- }- sort.Strings(keys)- if got := capKeys(keys); len(got) != api.MaxAnnounceKeys {- t.Errorf("capKeys: got %d, want %d", len(got), api.MaxAnnounceKeys)- }- if got := capKeys(keys[:3]); len(got) != 3 {- t.Errorf("capKeys under cap: got %d, want 3", len(got))- }-}--func TestGossipToolRows(t *testing.T) {- provs := []samdiscovery.Provider{- {PeerID: "peerA", Service: "reviewer", Labels: map[string]string{"region": "eu"}},- {PeerID: "peerB", Service: "other"},- }- rows := gossipToolRows(provs, "review_pr", "")- if len(rows) != 2 {- t.Fatalf("rows: got %+v, want 2", rows)- }- if rows[0].ToolName != "mcp://reviewer/review_pr" || rows[0].Labels["region"] != "eu" {- t.Errorf("row 0: %+v", rows[0])- }- if len(rows[1].Labels) != 0 {- t.Errorf("row 1 should have no labels: %+v", rows[1])- }-- filtered := gossipToolRows(provs, "review_pr", "mcp://reviewer")- if len(filtered) != 1 || filtered[0].PeerID != "peerA" {- t.Errorf("service filter: got %+v", filtered)- }-}--// The tool schema documents service_name as a bare name ("code-reviewer") while-// tool names carry the mcp:// prefix. Matching the two literally made the-// documented spelling return nothing, which is indistinguishable from a mesh-// that has no such service.-func TestNormalizeServiceFilter(t *testing.T) {- for _, tc := range []struct {- name string- in string- want string- wantErr bool- }{- {name: "empty is no filter", in: "", want: ""},- {name: "bare name gains the scheme", in: "everything", want: "mcp://everything"},- {name: "namespaced name is already canonical", in: "mcp://everything", want: "mcp://everything"},- {name: "a non-MCP scheme is an error, not an empty result", in: "inference://vllm-tpu", wantErr: true},- } {- t.Run(tc.name, func(t *testing.T) {- got, err := normalizeServiceFilter(tc.in)- if tc.wantErr {- if err == nil {- t.Fatalf("normalizeServiceFilter(%q): want error, got %q", tc.in, got)- }- return- }- if err != nil {- t.Fatalf("normalizeServiceFilter(%q): %v", tc.in, err)- }- if got != tc.want {- t.Errorf("normalizeServiceFilter(%q): got %q, want %q", tc.in, got, tc.want)- }- })- }-}--// Providers announce a service either bare or namespaced; both denote the same-// service and must match one canonical filter.-func TestGossipToolRows_MatchesEitherAnnouncedForm(t *testing.T) {- provs := []samdiscovery.Provider{- {PeerID: "peerA", Service: "everything"},- {PeerID: "peerB", Service: "mcp://everything"},- {PeerID: "peerC", Service: "other"},- }- rows := gossipToolRows(provs, "get-sum", "mcp://everything")- if len(rows) != 2 {- t.Fatalf("got %d rows, want 2: %+v", len(rows), rows)- }- for _, row := range rows {- if row.ToolName != "mcp://everything/get-sum" {- t.Errorf("got %q", row.ToolName)- }- }-}--func TestFilterRowsByToolName(t *testing.T) {- rows := []remoteToolRow{- {PeerID: "p1", ToolName: "mcp://svc/review_pr"},- {PeerID: "p2", ToolName: "mcp://svc/lint"},- {PeerID: "p3", Error: "unreachable"},- }- got := filterRowsByToolName(rows, "review_pr")- if len(got) != 1 || got[0].PeerID != "p1" {- t.Errorf("filter: got %+v", got)- }-}diff --git a/internal/node/mcp_handlers.go b/internal/node/mcp_handlers.goindex aee8ce6b..a6495d38 100644--- a/internal/node/mcp_handlers.go+++ b/internal/node/mcp_handlers.go@@ -24,7 +24,6 @@ import ("time""github.com/google/agentmesh/api"- samdiscovery "github.com/google/agentmesh/internal/node/discovery""github.com/libp2p/go-libp2p/core/peer""github.com/modelcontextprotocol/go-sdk/mcp")@@ -233,19 +232,6 @@ func (n *AgentMeshNode) handleFindRemoteTools(ctx context.Context, req *mcp.Callvar rows []remoteToolRow- // Exact-name lookup: gossip fast path, avoiding the catalog fan-out- // when interested announcements are already flowing.- if params.ToolName != "" && params.PeerID == "" && n.Discovery != nil {- n.Discovery.Ensure(api.ServiceType_SERVICE_TYPE_MCP, params.ToolName)- if provs := n.Discovery.Providers(api.ServiceType_SERVICE_TYPE_MCP, params.ToolName); len(provs) > 0 {- candidates := gossipToolRows(provs, params.ToolName, serviceFilter)- rows = n.verifyGossipToolRows(ctx, candidates)- if len(rows) > 0 {- return marshalToolRows(rows)- }- }- }-if params.PeerID != "" {pid, err := peer.Decode(params.PeerID)if err != nil {@@ -281,7 +267,6 @@ func (n *AgentMeshNode) handleFindRemoteTools(ctx context.Context, req *mcp.Callif params.ToolName != "" {rows = filterRowsByToolName(rows, params.ToolName)}- n.annotateToolLabels(rows)return marshalToolRows(rows)}@@ -319,119 +304,6 @@ func normalizeServiceFilter(name string) (string, error) {}}-// gossipToolRows builds result rows from gossip-observed providers of a tool.-// serviceNameFilter must already be canonical; see normalizeServiceFilter.-func gossipToolRows(provs []samdiscovery.Provider, toolName, serviceNameFilter string) []remoteToolRow {- var rows []remoteToolRow- for _, p := range provs {- name := p.Service- if !strings.Contains(name, "://") {- name = api.MCPServicePrefix + name- }- if serviceNameFilter != "" && name != serviceNameFilter {- continue- }- rows = append(rows, remoteToolRow{- PeerID: p.PeerID,- ToolName: name + "/" + toolName,- Labels: p.Labels,- })- }- return rows-}--// verifyGossipToolRows treats unsolicited announcements as routing hints, not-// authorization evidence. Each candidate is confirmed through the existing-// authenticated MCP session and exact tools/list response before it is exposed.-func (n *AgentMeshNode) 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- }-- type verificationResult struct {- row remoteToolRow- ok bool- }- results := make([]verificationResult, len(candidates))- jobs := make(chan int)- workerCount := len(candidates)- if workerCount > gossipVerificationMaxConcurrent {- workerCount = gossipVerificationMaxConcurrent- }-- var wg sync.WaitGroup- for range workerCount {- wg.Add(1)- go func() {- defer wg.Done()- for {- select {- case <-ctx.Done():- return- case index, ok := <-jobs:- if !ok {- return- }- candidate := candidates[index]- pid, err := peer.Decode(candidate.PeerID)- if err != nil {- logger.Debugf("[find_remote_tools] invalid gossip peer %q skipped: %v", candidate.PeerID, err)- continue- }- 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- }- results[index] = verificationResult{- ok: true,- row: remoteToolRow{- PeerID: description.PeerID,- ToolName: description.ToolName,- Description: description.Description,- Labels: candidate.Labels,- },- }- }- }- }()- }--sendCandidates:- for index := range candidates {- select {- case jobs <- index:- case <-ctx.Done():- break sendCandidates- }- }- close(jobs)- wg.Wait()-- rows := make([]remoteToolRow, 0, len(results))- for _, result := range results {- if result.ok {- rows = append(rows, result.row)- }- }- return rows-}-// filterRowsByToolName keeps rows whose namespaced tool name ends in the// bare tool name; error rows (no tool listing) are dropped from targeted// lookups.@@ -448,22 +320,6 @@ func filterRowsByToolName(rows []remoteToolRow, toolName string) []remoteToolRowreturn out}-// annotateToolLabels fills in the operator-declared labels for peers the-// gossip view knows, when not already known from a fresher source.-func (n *AgentMeshNode) annotateToolLabels(rows []remoteToolRow) {- if n.Discovery == nil {- return- }- for i := range rows {- if len(rows[i].Labels) != 0 {- continue- }- if labels := n.Discovery.PeerLabels(rows[i].PeerID); labels != nil {- rows[i].Labels = labels- }- }-}-// fetchRemoteToolCatalogue gets the remote node's service catalogue,// then opens a separate libp2p stream to each MCP service to fetch its tools.// serviceNameFilter must already be canonical; see normalizeServiceFilter.diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.goindex 91e4a29d..5ecc9c81 100644--- a/internal/node/mcp_handlers_test.go+++ b/internal/node/mcp_handlers_test.go@@ -19,13 +19,10 @@ import ("crypto/ed25519""crypto/rand""encoding/json"- "errors"- "fmt""io""net/http""net/http/httptest""strings"- "sync/atomic""testing""time"@@ -531,140 +528,6 @@ func TestFetchRemoteToolCatalogue_AuthRejectedHidden(t *testing.T) {}}-func TestVerifyGossipToolRows_AuthenticatesEachServiceOnSamePeer(t *testing.T) {- ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)- defer cancel()-- allowedSrv := httptest.NewServer(newFakeMCPHandler(t, []*mcp.Tool{- {Name: "review_pr", Description: "authorized review", InputSchema: map[string]any{"type": "object"}},- }))- defer allowedSrv.Close()- deniedSrv := httptest.NewServer(newFakeMCPHandler(t, []*mcp.Tool{- {Name: "review_pr", Description: "unauthorized review", InputSchema: map[string]any{"type": "object"}},- }))- defer deniedSrv.Close()-- nodeA, cleanupA := startBareNode(t, ctx)- defer cleanupA()- nodeB, cleanupB := startBareNode(t, ctx)- defer cleanupB()-- _, rootPriv := enrollUnderRoot(t, nodeA, nodeB)- if err := buildAndSaveCustomBiscuit(nodeA, rootPriv, []string{"mcp://allowed-reviewer"}); err != nil {- t.Fatalf("buildAndSaveCustomBiscuit: %v", err)- }- if err := nodeA.Host.Connect(ctx, peer.AddrInfo{ID: nodeB.Host.ID(), Addrs: nodeB.Host.Addrs()}); err != nil {- t.Fatal(err)- }-- for name, targetURL := range map[string]string{- "allowed-reviewer": allowedSrv.URL,- "denied-reviewer": deniedSrv.URL,- } {- if err := nodeB.RegisterService(ctx, &api.RegisterServiceRequest{- Service: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: name},- Backend: &api.RegisterServiceRequest_TargetUrl{TargetUrl: targetURL},- }); err != nil {- t.Fatalf("RegisterService %s: %v", name, err)- }- }-- peerID := nodeB.Host.ID().String()- rows := nodeA.verifyGossipToolRows(ctx, []remoteToolRow{- {PeerID: peerID, ToolName: "mcp://allowed-reviewer/review_pr", Labels: map[string]string{"region": "us"}},- {PeerID: peerID, ToolName: "mcp://denied-reviewer/review_pr", Labels: map[string]string{"region": "us"}},- })-- if len(rows) != 1 {- t.Fatalf("expected one authorized row, got %d: %+v", len(rows), rows)- }- if rows[0].ToolName != "mcp://allowed-reviewer/review_pr" {- t.Fatalf("unexpected verified tool: %+v", rows[0])- }- if rows[0].Description != "authorized review" {- t.Errorf("Description = %q, want authorized review", rows[0].Description)- }- if rows[0].Labels["region"] != "us" {- t.Errorf("gossip routing labels were not preserved: %+v", rows[0].Labels)- }-}--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()diff --git a/internal/node/node.go b/internal/node/node.goindex a5b5604a..6a09c92a 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -41,7 +41,6 @@ import ("github.com/google/agentmesh/api"cpclient "github.com/google/agentmesh/internal/controlplane/client""github.com/google/agentmesh/internal/identity"- samdiscovery "github.com/google/agentmesh/internal/node/discovery""github.com/google/agentmesh/internal/p2plimits""github.com/google/agentmesh/internal/ratelimit"lru "github.com/hashicorp/golang-lru/v2"@@ -149,7 +148,6 @@ type AgentMeshNode struct {Host host.HostDirectory DirectoryPubSub *pubsub.PubSub- Discovery *samdiscovery.DiscoveryStore *StoreRouterPeerID peer.IDauthenticatedRouters map[peer.ID]bool@@ -698,16 +696,19 @@ func (n *AgentMeshNode) Start(ctx context.Context) error {return err}- // Initialize Gossipsub for control plane events. StrictSign is the+ // Initialize pubsub for control plane events. StrictSign is the// library default; it is pinned because the event validator and the// per-author rate limit key on msg.GetFrom(), which only the signature// makes trustworthy.- ps, err := pubsub.NewGossipSub(ctx, gated, pubsub.WithMessageSignaturePolicy(pubsub.StrictSign))+ // A member is a leaf of the gossip: it speaks floodsub, so a router+ // delivers every event to it outright and it takes no part in the+ // routers' gossipsub mesh, its heartbeats, grafts and peer exchange.+ ps, err := pubsub.NewFloodSub(ctx, gated, pubsub.WithMessageSignaturePolicy(pubsub.StrictSign))if err != nil {return err}n.PubSub = ps- // Validate control-plane events before GossipSub accepts or re-forwards+ // Validate control-plane events before pubsub accepts or re-forwards// them: one ed25519 verify per message, so a peer flooding the topic with// junk costs itself the verify and never reaches the subscriber or the// next hop. Without this the rate limit ran first, keyed on the@@ -717,10 +718,6 @@ func (n *AgentMeshNode) Start(ctx context.Context) error {return fmt.Errorf("register mesh event validator: %w", err)}- // Interest-scoped service announcements (provider + consumer roles).- n.Discovery = samdiscovery.New(ps, h.ID())- n.Discovery.Start(ctx, n.discoverySource)-// Subscribe to local address updates to reprovide services and logsub, err := h.EventBus().Subscribe(new(event.EvtLocalAddressesUpdated))if err == nil {diff --git a/internal/node/openai_facade.go b/internal/node/openai_facade.goindex ed1cf69f..5c50f456 100644--- a/internal/node/openai_facade.go+++ b/internal/node/openai_facade.go@@ -123,37 +123,11 @@ func newOpenAIFacade(node *AgentMeshNode, egress http.Handler) *openAIFacade {remoteModels: func(ctx context.Context, peerID, srvName string) ([]string, error) {return fetchRemoteModels(ctx, node, client, peerID, srvName)},- viewProviders: func(model string) []modelProvider {- if node.Discovery == nil {- return nil- }- var out []modelProvider- for _, p := range node.Discovery.Providers(api.ServiceType_SERVICE_TYPE_INFERENCE, model) {- out = append(out, modelProvider{- peerID: p.PeerID,- service: p.Service,- labels: p.Labels,- active: p.Load.ActiveRequests,- })- }- return out- },- ensureInterest: func(model string) {- if node.Discovery != nil {- node.Discovery.Ensure(api.ServiceType_SERVICE_TYPE_INFERENCE, model)- }- },isRevoked: func(peerID string) bool {return node.revokedPeers != nil && node.revokedPeers.Contains(peerID)},localLabels: node.labels,egressFloor: node.egressFloor,- peerLabels: func(peerID string) map[string]string {- if node.Discovery == nil {- return nil- }- return node.Discovery.PeerLabels(peerID)- },verifyPeerLabels: func(ctx context.Context, peerID string, required map[string]string) error {pid, err := peer.Decode(peerID)if err != nil {