Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node, router: gossip validation, relay ACL, callee verification (audit PR 5)
Antonio Ojeab0c5c3c8cc95
diff --git a/internal/node/discovery/discovery.go b/internal/node/discovery/discovery.goindex a77a985f..2a32f552 100644--- a/internal/node/discovery/discovery.go+++ b/internal/node/discovery/discovery.go@@ -41,6 +41,11 @@ const (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.Minutediff --git a/internal/node/discovery/discovery_test.go b/internal/node/discovery/discovery_test.goindex 936b9650..0cbd0df1 100644--- a/internal/node/discovery/discovery_test.go+++ b/internal/node/discovery/discovery_test.go@@ -235,6 +235,38 @@ func TestProviderTableIsBounded(t *testing.T) {}}+// 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"},+ Timestamp: time.Now().Unix(),+ }))++ 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"},+ Timestamp: time.Now().Unix(),+ }))+ }++ 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)diff --git a/internal/node/discovery/view.go b/internal/node/discovery/view.goindex bc4e6cfe..c5f18cc5 100644--- a/internal/node/discovery/view.go+++ b/internal/node/discovery/view.go@@ -132,8 +132,14 @@ func (d *Discovery) observe(msg *pubsub.Message) {d.viewMu.Lock()defer d.viewMu.Unlock()- if _, exists := d.providers[entryKey]; !exists && len(d.providers) >= d.maxProviders {- d.evictOldestLocked()+ 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,@@ -149,10 +155,25 @@ func (d *Discovery) observe(msg *pubsub.Message) {}}-func (d *Discovery) evictOldestLocked() {+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 stringvar oldest time.Timefor k, p := range d.providers {+ if signer != "" && p.PeerID != signer {+ continue+ }if oldestKey == "" || p.LastSeen.Before(oldest) {oldestKey, oldest = k, p.LastSeen}diff --git a/internal/node/gossip_hardening_test.go b/internal/node/gossip_hardening_test.gonew file mode 100644index 00000000..7b5c7886--- /dev/null+++ b/internal/node/gossip_hardening_test.go@@ -0,0 +1,260 @@+// 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"+ "crypto/ed25519"+ "crypto/rand"+ "fmt"+ "slices"+ "sync"+ "testing"+ "time"++ "github.com/google/sam/api"+ "github.com/google/sam/internal/ratelimit"+ "github.com/libp2p/go-libp2p"+ 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/host"+ "github.com/libp2p/go-libp2p/core/peer"+ "google.golang.org/protobuf/proto"+)++// signedMeshEvent returns a BANNED event for target signed by cpPriv, as the+// control plane would publish it.+func signedMeshEvent(t *testing.T, cpPriv ed25519.PrivateKey, target peer.ID, at time.Time) []byte {+ t.Helper()+ event := &api.MeshEvent{Type: api.MeshEvent_BANNED, PeerId: target.String(), Timestamp: at.UnixMilli()}+ unsigned, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)+ if err != nil {+ t.Fatal(err)+ }+ event.Signature = ed25519.Sign(cpPriv, unsigned)+ data, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)+ if err != nil {+ t.Fatal(err)+ }+ return data+}++func randomPeerID(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+}++// The GossipSub validator is the first thing a mesh event meets. It has to+// reject (drop, do not forward) what is unsigned or forged, ignore what is+// stale, and accept a fresh event from a trusted control plane.+func TestValidateMeshEvent(t *testing.T) {+ cpPub, cpPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ _, otherPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ node := &SamNode{+ trustedKeys: []TrustedKey{{Key: cpPub, ReceivedAt: time.Now()}},+ BiscuitTimeout: 500 * time.Millisecond,+ }+ from := randomPeerID(t)+ target := randomPeerID(t)++ tests := []struct {+ name string+ data []byte+ want pubsub.ValidationResult+ }{+ {"fresh event from trusted control plane", signedMeshEvent(t, cpPriv, target, time.Now()), pubsub.ValidationAccept},+ {"event signed by an untrusted key", signedMeshEvent(t, otherPriv, target, time.Now()), pubsub.ValidationReject},+ {"undecodable payload", []byte("junk"), pubsub.ValidationReject},+ {"stale event", signedMeshEvent(t, cpPriv, target, time.Now().Add(-2*FreshnessThreshold)), pubsub.ValidationIgnore},+ {"future event", signedMeshEvent(t, cpPriv, target, time.Now().Add(2*FreshnessThreshold)), pubsub.ValidationIgnore},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ msg := &pubsub.Message{Message: &pubsub_pb.Message{From: []byte(from), Data: tt.data}}+ if got := node.validateMeshEvent(context.Background(), from, msg); got != tt.want {+ t.Errorf("validateMeshEvent = %v, want %v", got, tt.want)+ }+ })+ }+}++// gossipPeer is a plain libp2p host with its own GossipSub and no validator:+// any peer that can reach the topic, or a router forwarding one. It is+// subscribed so it is a topic peer to whoever it connects to.+type gossipPeer struct {+ host host.Host+ ps *pubsub.PubSub+ topic *pubsub.Topic+ sub *pubsub.Subscription+}++func newGossipPeer(t *testing.T, ctx context.Context) *gossipPeer {+ t.Helper()+ h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = h.Close() })+ ps, err := pubsub.NewGossipSub(ctx, h)+ if err != nil {+ t.Fatal(err)+ }+ topic, err := ps.Join(api.GossipEvents)+ if err != nil {+ t.Fatal(err)+ }+ sub, err := topic.Subscribe()+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(sub.Cancel)+ return &gossipPeer{host: h, ps: ps, topic: topic, sub: sub}+}++func (g *gossipPeer) connect(t *testing.T, ctx context.Context, h host.Host) {+ t.Helper()+ if err := g.host.Connect(ctx, peer.AddrInfo{ID: h.ID(), Addrs: h.Addrs()}); err != nil {+ t.Fatalf("connect %s -> %s: %v", g.host.ID(), h.ID(), err)+ }+}++func waitUntil(t *testing.T, timeout time.Duration, what string, cond func() bool) {+ t.Helper()+ deadline := time.Now().Add(timeout)+ for time.Now().Before(deadline) {+ if cond() {+ return+ }+ time.Sleep(50 * time.Millisecond)+ }+ t.Fatalf("timed out waiting for %s", what)+}++// A flood of junk on the events topic must neither be re-forwarded by the+// node nor cost it the signed event that follows. Before the validator the+// node forwarded every message and rate-limited on the forwarding peer, so a+// flood via the router spent the router's budget and real bans were dropped.+func TestGossipJunkFloodIsRejectedNotForwarded(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)+ defer cancel()++ victim, cleanup := startBareNode(t, ctx)+ defer cleanup()++ cpPub, cpPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ victim.keysMu.Lock()+ victim.trustedKeys = append(victim.trustedKeys, TrustedKey{Key: cpPub, ReceivedAt: time.Now()})+ victim.keysMu.Unlock()++ // attacker -> victim -> observer. The observer only ever hears what the+ // victim chooses to forward.+ attacker := newGossipPeer(t, ctx)+ observer := newGossipPeer(t, ctx)+ attacker.connect(t, ctx, victim.Host)+ observer.connect(t, ctx, victim.Host)++ var mu sync.Mutex+ var forwardedJunk []string+ seenSigned := map[peer.ID]bool{}+ sawSigned := func(pid peer.ID) bool {+ mu.Lock()+ defer mu.Unlock()+ return seenSigned[pid]+ }+ go func() {+ for {+ msg, err := observer.sub.Next(ctx)+ if err != nil {+ return+ }+ // Only what came through the victim counts: a direct+ // attacker->observer connection would say nothing about the+ // victim.+ if msg.ReceivedFrom != victim.Host.ID() {+ continue+ }+ mu.Lock()+ var event api.MeshEvent+ if err := proto.Unmarshal(msg.Data, &event); err != nil || len(event.Signature) == 0 {+ forwardedJunk = append(forwardedJunk, string(msg.Data))+ } else if pid, err := peer.Decode(event.PeerId); err == nil {+ seenSigned[pid] = true+ }+ mu.Unlock()+ }+ }()++ waitUntil(t, 10*time.Second, "topic peers to see each other", func() bool {+ victimSees := victim.PubSub.ListPeers(api.GossipEvents)+ return slices.Contains(victimSees, attacker.host.ID()) &&+ slices.Contains(victimSees, observer.host.ID()) &&+ slices.Contains(attacker.ps.ListPeers(api.GossipEvents), victim.Host.ID()) &&+ slices.Contains(observer.ps.ListPeers(api.GossipEvents), victim.Host.ID())+ })++ // Probe: a signed event travels attacker -> victim -> observer, so the+ // forwarding path is up before the flood starts.+ first := randomPeerID(t)+ waitUntil(t, 10*time.Second, "probe event to be forwarded", func() bool {+ _ = attacker.topic.Publish(ctx, signedMeshEvent(t, cpPriv, first, time.Now()))+ return sawSigned(first)+ })++ // Flood: far more than the per-peer budget, all unsigned.+ for i := range 5 * ratelimit.PeerBurst {+ if err := attacker.topic.Publish(ctx, fmt.Appendf(nil, "junk-%d", i)); err != nil {+ t.Fatalf("publish junk: %v", err)+ }+ }++ // The event that matters, from the same forwarding peer, right behind+ // the flood.+ second := randomPeerID(t)+ if err := attacker.topic.Publish(ctx, signedMeshEvent(t, cpPriv, second, time.Now())); err != nil {+ t.Fatal(err)+ }+ waitUntil(t, 10*time.Second, "signed event to be forwarded after the flood", func() bool {+ return sawSigned(second)+ })+ if !victim.revokedPeers.Contains(second.String()) {+ t.Error("the signed ban behind the flood was not applied by the node")+ }++ // Anything the victim forwarded is ordered on the observer's stream, so+ // junk it let through has arrived by now.+ mu.Lock()+ defer mu.Unlock()+ if len(forwardedJunk) != 0 {+ t.Errorf("victim forwarded %d unsigned messages; the validator must drop them at the first hop (e.g. %q)", len(forwardedJunk), forwardedJunk[0])+ }+}diff --git a/internal/node/labels_gate.go b/internal/node/labels_gate.goindex f509aaea..80015913 100644--- a/internal/node/labels_gate.go+++ b/internal/node/labels_gate.go@@ -144,9 +144,9 @@ func (n *SamNode) egressFloor() map[string]string {// constrained does not get to opt out of it by saying nothing.func (n *SamNode) VerifyPeerLabels(ctx context.Context, peerID peer.ID, required map[string]string) error {floor := n.egressFloor()- if len(required) == 0 && len(floor) == 0 {- return nil- }+ // No early return for an empty requirement: with nothing to attest this+ // still verifies that the peer holds a control-plane-signed biscuit bound+ // to it, which is what makes a discovered peer a provider at all.key := labelGateKey(peerID, required, floor)if until, ok := n.peerLabelGate.Get(key); ok && time.Now().Before(until) {return nil@@ -170,7 +170,7 @@ func (n *SamNode) VerifyPeerLabels(ctx context.Context, peerID peer.ID, requiredfunc (n *SamNode) checkPeerLabels(providerBiscuit []byte, peerID peer.ID, required map[string]string) error {floor := n.egressFloor()if len(providerBiscuit) == 0 {- return fmt.Errorf("provider %s returned no identity biscuit; cannot attest required labels %v (egress floor %v)", peerID, required, floor)+ return fmt.Errorf("provider %s returned no identity biscuit; not an enrolled peer (required labels %v, egress floor %v)", peerID, required, floor)}n.keysMu.RLock()@@ -187,6 +187,11 @@ func (n *SamNode) checkPeerLabels(providerBiscuit []byte, peerID peer.ID, requirif err != nil {return fmt.Errorf("provider %s biscuit verification failed: %w", peerID, err)}+ // Only nodes host services. A router's or an admin's biscuit is a valid+ // mesh identity but not a provider, and must not be dialled as one.+ if err := identity.RequireRole(b, key, api.RoleNode, n.BiscuitTimeout); err != nil {+ return fmt.Errorf("provider %s is not enrolled as a node: %w", peerID, err)+ }authorizer, err := b.Authorizer(key, identity.AuthorizerOptions(n.BiscuitTimeout)...)if err != nil {diff --git a/internal/node/labels_gate_test.go b/internal/node/labels_gate_test.goindex 31c0c5f5..ebf20f83 100644--- a/internal/node/labels_gate_test.go+++ b/internal/node/labels_gate_test.go@@ -87,6 +87,21 @@ func TestCheckPeerLabels(t *testing.T) {t.Error("expected error, got nil")}})++ // A router's biscuit is a valid, trusted, peer-bound identity, but a+ // router is not a service provider; only role(node) may be dialled as one.+ t.Run("router biscuit is not a provider", func(t *testing.T) {+ routerTok, err := identity.MintBootstrapBiscuitToken(cpPriv, providerPeer, api.RoleRouter, expiry, nil, nil)+ if err != nil {+ t.Fatal(err)+ }+ if err := node.checkPeerLabels(routerTok, providerPeer, nil); err == nil {+ t.Error("a router's biscuit must not pass the provider gate")+ }+ if err := node.checkPeerLabels(mint(cpPriv, providerPeer, nil), providerPeer, nil); err != nil {+ t.Errorf("a node's biscuit with no requirement must pass: %v", err)+ }+ })}// The egress floor is the operator's, and a caller cannot waive it by asking@@ -188,11 +203,12 @@ func TestCheckPeerLabelsEnforcesEgressFloor(t *testing.T) {}}-// VerifyPeerLabels short-circuits when there is nothing to check. With a floor-// configured there always is, so the short-circuit must not swallow it: the-// gate has to run even for a caller that asked for nothing, which is the-// difference between a floor and a suggestion.-func TestVerifyPeerLabelsDoesNotShortCircuitPastTheFloor(t *testing.T) {+// VerifyPeerLabels has no short-circuit: with nothing to attest it still has+// to establish that the peer is an enrolled member, and with a floor+// configured there is always something to attest. Either way a caller that+// asked for nothing is gated, which is the difference between a floor and a+// suggestion, and between a provider and a peer that merely announced.+func TestVerifyPeerLabelsDoesNotShortCircuit(t *testing.T) {node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,nodeConfig: &NodeConfigComplete{EgressRequireLabels: map[string]string{"jurisdiction": "eu"}},@@ -210,10 +226,11 @@ func TestVerifyPeerLabelsDoesNotShortCircuitPastTheFloor(t *testing.T) {t.Fatal("a caller requiring nothing must still be gated when a floor is configured")}- // Without a floor, the same call is the documented no-op.+ // Without a floor the gate still runs: an unenrolled peer is not a+ // provider just because nobody asked for a label.node.nodeConfig = &NodeConfigComplete{}- if err := node.VerifyPeerLabels(context.Background(), peer.ID("some-peer"), nil); err != nil {- t.Errorf("with no floor and no requirement the gate must not run: %v", err)+ if err := node.VerifyPeerLabels(context.Background(), peer.ID("some-peer"), nil); err == nil {+ t.Error("with no floor and no requirement the gate must still verify the peer's identity")}}diff --git a/internal/node/mcp.go b/internal/node/mcp.goindex 45053ce0..bb0bac35 100644--- a/internal/node/mcp.go+++ b/internal/node/mcp.go@@ -77,24 +77,6 @@ func NewMCPServer(node *SamNode) *mcp.Server {Description: "Discover remote services in the mesh. Provide only `type` to browse every reachable service of that type (returns name + description for each); add `name` to target a specific service. For `type: inference`, each result's `local_proxy_url` is called directly over HTTP (NOT via call_remote_tool) — the response includes a usage hint with the exact headers required.",}, node.handleDiscoverRemoteServices)- // Add the mesh_pubsub_broadcast tool.- mcp.AddTool(mcpServer, &mcp.Tool{- Name: "mesh_pubsub_broadcast",- Description: "Publish an event payload to a custom GossipSub topic",- }, node.handleMeshPubsubBroadcast)-- // Add the poll_messages tool.- mcp.AddTool(mcpServer, &mcp.Tool{- Name: "poll_messages",- Description: "Poll for incoming messages on custom GossipSub topics",- }, node.handlePollMessages)-- // Add the subscribe_topic tool.- mcp.AddTool(mcpServer, &mcp.Tool{- Name: "subscribe_topic",- Description: "Subscribe to a custom GossipSub topic",- }, node.handleSubscribeTopic)-// Add the get_mesh_info tool.mcp.AddTool(mcpServer, &mcp.Tool{Name: "get_mesh_info",@@ -378,14 +360,13 @@ func (n *SamNode) ConnectMCPSession(ctx context.Context, targetPeer peer.ID, tarreturn nil, nil, fmt.Errorf("%w by %s: %s", ErrAuthRejected, targetPeer, resp.Error)}- // The gate runs when the caller requires labels or when the operator's- // egress floor does: a caller that requires nothing is still held to the- // floor (checkPeerLabels ANDs both).- if len(requiredLabels) > 0 || len(n.egressFloor()) > 0 {- if err := n.checkPeerLabels(resp.Biscuit, targetPeer, requiredLabels); err != nil {- cleanup()- return nil, nil, err- }+ // The provider's biscuit is verified unconditionally: signature, expiry and+ // binding to targetPeer. Discovery (DHT, gossip) names whoever announced+ // the service; only this says the peer is enrolled. Caller-required labels+ // and the operator's egress floor are additional checks on the same token.+ if err := n.checkPeerLabels(resp.Biscuit, targetPeer, requiredLabels); err != nil {+ cleanup()+ return nil, nil, err}// Handoff to SDK using custom transportdiff --git a/internal/node/mcp_handlers.go b/internal/node/mcp_handlers.goindex 60e081ae..ce61fd39 100644--- a/internal/node/mcp_handlers.go+++ b/internal/node/mcp_handlers.go@@ -115,74 +115,6 @@ func (n *SamNode) handleDiscoverRemoteServices(ctx context.Context, req *mcp.Cal}, nil, nil}-// MeshPubsubBroadcastParams defines the parameters for the mesh_pubsub_broadcast tool.-type MeshPubsubBroadcastParams struct {- Topic string `json:"topic" jsonschema:"GossipSub topic name"`- Payload string `json:"payload" jsonschema:"Payload to publish"`-}--// handleMeshPubsubBroadcast implements the mesh_pubsub_broadcast tool.-func (n *SamNode) handleMeshPubsubBroadcast(ctx context.Context, req *mcp.CallToolRequest, params MeshPubsubBroadcastParams) (*mcp.CallToolResult, any, error) {- n.mu.Lock()- t, ok := n.topics[params.Topic]- var err error- if !ok {- t, err = n.PubSub.Join(params.Topic)- if err == nil {- n.topics[params.Topic] = t- }- }- n.mu.Unlock()- if err != nil {- return nil, nil, err- }- if err := t.Publish(ctx, []byte(params.Payload)); err != nil {- return nil, nil, err- }- return &mcp.CallToolResult{- Content: []mcp.Content{- &mcp.TextContent{Text: "Published"},- },- }, nil, nil-}--// PollMessagesParams defines the parameters for the poll_messages tool.-type PollMessagesParams struct {- Topic string `json:"topic" jsonschema:"GossipSub topic name"`-}--// handlePollMessages implements the poll_messages tool.-func (n *SamNode) handlePollMessages(ctx context.Context, req *mcp.CallToolRequest, params PollMessagesParams) (*mcp.CallToolResult, any, error) {- n.mu.Lock()- msgs := n.receivedMsgs[params.Topic]- delete(n.receivedMsgs, params.Topic) // Clear on read!- n.mu.Unlock()-- response := fmt.Sprintf("Messages on topic %s: %v", params.Topic, msgs)- return &mcp.CallToolResult{- Content: []mcp.Content{- &mcp.TextContent{Text: response},- },- }, nil, nil-}--// SubscribeTopicParams defines the parameters for the subscribe_topic tool.-type SubscribeTopicParams struct {- Topic string `json:"topic" jsonschema:"GossipSub topic name"`-}--// handleSubscribeTopic implements the subscribe_topic tool.-func (n *SamNode) handleSubscribeTopic(ctx context.Context, req *mcp.CallToolRequest, params SubscribeTopicParams) (*mcp.CallToolResult, any, error) {- if err := n.subscribeToTopic(ctx, params.Topic); err != nil {- return nil, nil, err- }- return &mcp.CallToolResult{- Content: []mcp.Content{- &mcp.TextContent{Text: "Subscribed"},- },- }, nil, nil-}-// GetMeshInfoParams defines the parameters for the get_mesh_info tool.type GetMeshInfoParams struct{}diff --git a/internal/node/mcp_handlers_additional_test.go b/internal/node/mcp_handlers_additional_test.goindex fc120d8f..423ecc67 100644--- a/internal/node/mcp_handlers_additional_test.go+++ b/internal/node/mcp_handlers_additional_test.go@@ -57,43 +57,6 @@ func TestHandleDiscoverRemoteServices(t *testing.T) {}}-func TestHandleMeshPubsub(t *testing.T) {- ctx := context.Background()- node, cleanup := startBareNode(t, ctx)- defer cleanup()-- // Subscribe- res, _, err := node.handleSubscribeTopic(context.Background(), &mcp.CallToolRequest{}, SubscribeTopicParams{- Topic: "test-topic",- })- if err != nil {- t.Fatalf("handleSubscribeTopic failed: %v", err)- }- if res.Content[0].(*mcp.TextContent).Text != "Subscribed" {- t.Errorf("expected Subscribed")- }-- // Publish- _, _, err = node.handleMeshPubsubBroadcast(context.Background(), &mcp.CallToolRequest{}, MeshPubsubBroadcastParams{- Topic: "test-topic",- Payload: "test-message",- })- if err != nil {- t.Fatalf("handleMeshPubsubBroadcast failed: %v", err)- }-- // Poll- res, _, err = node.handlePollMessages(context.Background(), &mcp.CallToolRequest{}, PollMessagesParams{- Topic: "test-topic",- })- if err != nil {- t.Fatalf("handlePollMessages failed: %v", err)- }- if len(res.Content) == 0 {- t.Fatalf("expected content in poll messages")- }-}-func TestHandleGetMeshInfo(t *testing.T) {ctx := context.Background()node, cleanup := startBareNode(t, ctx)diff --git a/internal/node/mcp_handlers_test.go b/internal/node/mcp_handlers_test.goindex 4ea0a7e8..f53f66fa 100644--- a/internal/node/mcp_handlers_test.go+++ b/internal/node/mcp_handlers_test.go@@ -38,7 +38,8 @@ import ()// buildAndSaveBiscuit builds a biscuit signed with rootPriv that identifies-// node as caller, grants allow_mcp_server("*"), and saves it to node's store.+// node as caller, grants allow_mcp_server("*"), carries the node role as a+// real enrollment does, and saves it to node's store.func buildAndSaveBiscuit(node *SamNode, rootPriv ed25519.PrivateKey) error {callerID := node.Host.ID().String()builder := biscuit.NewBuilder(rootPriv)@@ -49,6 +50,12 @@ func buildAndSaveBiscuit(node *SamNode, rootPriv ed25519.PrivateKey) error {}}); err != nil {return err}+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(api.RoleNode)},+ }}); err != nil {+ return err+ }if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "client_peer_id",IDs: []biscuit.Term{biscuit.String(callerID)},@@ -78,6 +85,28 @@ func buildAndSaveBiscuit(node *SamNode, rootPriv ed25519.PrivateKey) error {return node.Store.SaveIdentity(biscBytes)}+// enrollUnderRoot makes every node a member of the same mesh: each gets an+// identity biscuit signed by a fresh root and trusts that root, so both ends+// of any pair verify each other (the caller through WithBiscuitAuth, the+// provider through the label gate). Returns the root key pair for tests that+// need to mint extra tokens.+func enrollUnderRoot(t *testing.T, nodes ...*SamNode) (ed25519.PublicKey, ed25519.PrivateKey) {+ t.Helper()+ rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatalf("gen root key: %v", err)+ }+ for _, n := range nodes {+ if err := buildAndSaveBiscuit(n, rootPriv); err != nil {+ t.Fatalf("buildAndSaveBiscuit for %s: %v", n.Host.ID(), err)+ }+ n.keysMu.Lock()+ n.trustedKeys = append(n.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})+ n.keysMu.Unlock()+ }+ return rootPub, rootPriv+}+func TestHandleFindRemoteTools_EmptyMesh_ReturnsEmptyArray(t *testing.T) {ctx, cancel := contextWithShortTimeout()defer cancel()@@ -146,18 +175,7 @@ func TestHandleFindRemoteTools_SinglePeer(t *testing.T) {t.Fatalf("connect: %v", err)}- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root key: %v", err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }-- // B trusts the same root key used to sign A's biscuit.- nodeB.keysMu.Lock()- nodeB.trustedKeys = append(nodeB.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeB.keysMu.Unlock()+ enrollUnderRoot(t, nodeA, nodeB)// Register an MCP service on B with two tools.regReq := &api.RegisterServiceRequest{@@ -231,16 +249,7 @@ func TestHandleFindRemoteTools_BackendPredatesDiscover(t *testing.T) {t.Fatalf("connect: %v", err)}- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root key: %v", err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeB.keysMu.Lock()- nodeB.trustedKeys = append(nodeB.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeB.keysMu.Unlock()+ enrollUnderRoot(t, nodeA, nodeB)regReq := &api.RegisterServiceRequest{Service: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "calculator"},@@ -309,21 +318,13 @@ func TestHandleFindRemoteTools_MeshWide(t *testing.T) {nodeD, cleanupD := startBareNode(t, ctx)defer cleanupD()- // Connect A to B, C, and D, and set up biscuit auth so A's stream passes WithBiscuitAuth.- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root: %v", err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }+ // Connect A to B, C, and D; every node is enrolled under one root so A's+ // stream passes WithBiscuitAuth and each target passes A's provider gate.+ enrollUnderRoot(t, nodeA, nodeB, nodeC, nodeD)for _, target := range []*SamNode{nodeB, nodeC, nodeD} {if err := nodeA.Host.Connect(ctx, peer.AddrInfo{ID: target.Host.ID(), Addrs: target.Host.Addrs()}); err != nil {t.Fatalf("connect to %s: %v", target.Host.ID(), err)}- target.keysMu.Lock()- target.trustedKeys = append(target.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- target.keysMu.Unlock()}if err := nodeB.RegisterService(ctx, &api.RegisterServiceRequest{@@ -399,21 +400,12 @@ func TestHandleFindRemoteTools_PartialFailure(t *testing.T) {nodeC, cleanupC := startBareNode(t, ctx)defer cleanupC()- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatal(err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }+ enrollUnderRoot(t, nodeA, nodeC)// A connects to C only; B is a fictional peer ID that won't resolve.if err := nodeA.Host.Connect(ctx, peer.AddrInfo{ID: nodeC.Host.ID(), Addrs: nodeC.Host.Addrs()}); err != nil {t.Fatal(err)}- nodeC.keysMu.Lock()- nodeC.trustedKeys = append(nodeC.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeC.keysMu.Unlock()if err := nodeC.RegisterService(ctx, &api.RegisterServiceRequest{Service: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "summarizer"},@@ -458,6 +450,12 @@ func buildAndSaveCustomBiscuit(node *SamNode, rootPriv ed25519.PrivateKey, allow}}); err != nil {return err}+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(api.RoleNode)},+ }}); err != nil {+ return err+ }if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "client_peer_id",IDs: []biscuit.Term{biscuit.String(callerID)},@@ -507,21 +505,14 @@ func TestFetchRemoteToolCatalogue_AuthRejectedHidden(t *testing.T) {nodeC, cleanupC := startBareNode(t, ctx)defer cleanupC()- rootPubEd, rootPrivEd, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatal(err)- }-- // Create a biscuit that DOES NOT allow "summarizer", only "some_other_service" and "system://sam.catalog".+ // Both enrolled under one root; A's identity is then narrowed to a+ // biscuit that DOES NOT allow "summarizer", only "some_other_service"+ // and "system://sam.catalog".+ _, rootPrivEd := enrollUnderRoot(t, nodeA, nodeC)if err := buildAndSaveCustomBiscuit(nodeA, rootPrivEd, []string{"mcp://some_other_service", "system://sam.catalog"}); err != nil {t.Fatalf("buildAndSaveCustomBiscuit: %v", err)}- // Trust the key in Node C- nodeC.keysMu.Lock()- nodeC.trustedKeys = append(nodeC.trustedKeys, TrustedKey{Key: rootPubEd, ReceivedAt: time.Now()})- nodeC.keysMu.Unlock()-if err := nodeA.Host.Connect(ctx, peer.AddrInfo{ID: nodeC.Host.ID(), Addrs: nodeC.Host.Addrs()}); err != nil {t.Fatal(err)}@@ -564,16 +555,10 @@ func TestVerifyGossipToolRows_AuthenticatesEachServiceOnSamePeer(t *testing.T) {nodeB, cleanupB := startBareNode(t, ctx)defer cleanupB()- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatal(err)- }+ _, rootPriv := enrollUnderRoot(t, nodeA, nodeB)if err := buildAndSaveCustomBiscuit(nodeA, rootPriv, []string{"mcp://allowed-reviewer"}); err != nil {t.Fatalf("buildAndSaveCustomBiscuit: %v", err)}- nodeB.keysMu.Lock()- nodeB.trustedKeys = append(nodeB.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeB.keysMu.Unlock()if err := nodeA.Host.Connect(ctx, peer.AddrInfo{ID: nodeB.Host.ID(), Addrs: nodeB.Host.Addrs()}); err != nil {t.Fatal(err)}@@ -942,16 +927,7 @@ func TestHandleDescribeRemoteTool_RoundTrip(t *testing.T) {t.Fatalf("connect: %v", err)}- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root key: %v", err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeB.keysMu.Lock()- nodeB.trustedKeys = append(nodeB.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeB.keysMu.Unlock()+ enrollUnderRoot(t, nodeA, nodeB)tools := []*mcp.Tool{{@@ -1038,16 +1014,7 @@ func TestHandleDescribeRemoteTool_RoundTrip_UnknownTool(t *testing.T) {t.Fatalf("connect: %v", err)}- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root key: %v", err)- }- if err := buildAndSaveBiscuit(nodeA, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeB.keysMu.Lock()- nodeB.trustedKeys = append(nodeB.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeB.keysMu.Unlock()+ enrollUnderRoot(t, nodeA, nodeB)tools := []*mcp.Tool{{Name: "review_pr", Description: "x", InputSchema: map[string]any{"type": "object"}},@@ -1062,7 +1029,7 @@ func TestHandleDescribeRemoteTool_RoundTrip_UnknownTool(t *testing.T) {}defer func() { _ = nodeB.UnregisterService(ctx, "code-reviewer") }()- _, _, err = nodeA.handleDescribeRemoteTool(ctx, &mcp.CallToolRequest{}, DescribeRemoteToolParams{+ _, _, err := nodeA.handleDescribeRemoteTool(ctx, &mcp.CallToolRequest{}, DescribeRemoteToolParams{PeerID: nodeB.Host.ID().String(),ToolName: "mcp://code-reviewer/does-not-exist",})diff --git a/internal/node/node.go b/internal/node/node.goindex f2c91186..6ac1cb65 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -121,7 +121,9 @@ func (a *nodeRelayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {}func (a *nodeRelayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {- return a.node.isAdmitted(dest)+ // Both ends: an unauthenticated source could otherwise open circuits to+ // every admitted peer through this node.+ return a.node.isAdmitted(src) && a.node.isAdmitted(dest)}// isAdmitted reports whether a peer completed the auth handshake and its token@@ -150,8 +152,6 @@ type SamNode struct {RouterPeerID peer.IDauthenticatedRouters map[peer.ID]boolpeerLastEventTime map[string]int64- receivedMsgs map[string][]string- topics map[string]*pubsub.Topicmu sync.MutexnodeConfig *NodeConfigCompleterevokedPeers *lru.Cache[string, int64]@@ -284,8 +284,6 @@ func NewSamNode(cfg Options) (*SamNode, error) {Store: cfg.Store,trustedKeys: trustedKeys,peerLastEventTime: make(map[string]int64),- receivedMsgs: make(map[string][]string),- topics: make(map[string]*pubsub.Topic),authenticatedRouters: make(map[peer.ID]bool),nodeConfig: cfg.NodeConfig,AllowLoopback: cfg.AllowLoopback,@@ -567,12 +565,24 @@ func (n *SamNode) Start(ctx context.Context) error {}}- // Initialize Gossipsub for control plane events- ps, err := pubsub.NewGossipSub(ctx, h)+ // Initialize Gossipsub 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, h, pubsub.WithMessageSignaturePolicy(pubsub.StrictSign))if err != nil {return err}n.PubSub = ps+ // Validate control-plane events before GossipSub 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+ // forwarding peer, and a flood via the router exhausted the router's+ // budget so real ban and rotation events were dropped.+ if err := ps.RegisterTopicValidator(api.GossipEvents, n.validateMeshEvent); err != nil {+ return fmt.Errorf("register mesh event validator: %w", err)+ }// Interest-scoped service announcements (provider + consumer roles).n.Discovery = samdiscovery.New(ps, h.ID())@@ -1232,8 +1242,12 @@ func (n *SamNode) listenForControlPlaneEvents(ctx context.Context) {return}- if !n.rateLimiter.Allow(msg.ReceivedFrom.String()) {- logger.Warnw("[Mesh Event] rate limit exceeded, dropping message", "event", meshEventRateLimitDrop, "peer", msg.ReceivedFrom.String())+ // validateMeshEvent already rejected anything unsigned, misspelled or+ // stale; what arrives here is a genuine control-plane event. The limit+ // is on the author, so a burst from one control plane cannot be+ // blamed on the router that relayed it.+ if !n.rateLimiter.Allow(msg.GetFrom().String()) {+ logger.Warnw("[Mesh Event] rate limit exceeded, dropping message", "event", meshEventRateLimitDrop, "peer", msg.GetFrom().String())continue}@@ -1243,24 +1257,6 @@ func (n *SamNode) listenForControlPlaneEvents(ctx context.Context) {continue}- // Since the signature is verified against our list of trusted control plane public keys- // in verifyEvent below, any message with a valid signature is cryptographically- // proven to have been authored by one of the control planes. We do not restrict msg.GetFrom()- // to a single RouterPeerID because there can be multiple control plane replicas in a cluster,- // each with its own PeerID.-- if !n.verifyEvent(&event) {- logger.Warnw("[Mesh Event] potential spoofing attempt: invalid event signature", "event", meshEventSpoofingAttempt, "peer", msg.ReceivedFrom.String())- continue- }-- // Freshness check: reject events older than the threshold to prevent replay attacks- eventTime := time.UnixMilli(event.Timestamp)- if time.Since(eventTime) > FreshnessThreshold || time.Until(eventTime) > FreshnessThreshold {- logger.Warnw("[Mesh Event] dropping stale or future event", "event", meshEventStaleEvent, "peer", msg.ReceivedFrom.String(), "timestamp", event.Timestamp)- continue- }-switch event.Type {case api.MeshEvent_BANNED:n.handleBannedEvent(&event)@@ -1440,45 +1436,28 @@ func (n *SamNode) verifyEvent(event *api.MeshEvent) bool {return false}-func (n *SamNode) subscribeToTopic(ctx context.Context, topicName string) error {- n.mu.Lock()- defer n.mu.Unlock()-- if _, ok := n.topics[topicName]; ok {- return nil- }-- topic, err := n.PubSub.Join(topicName)- if err != nil {- return err- }-- sub, err := topic.Subscribe()- if err != nil {- return err- }-- n.topics[topicName] = topic-- logger.Infof("[PubSub] Started subscription background loop for topic: %s", topicName)- go func() {- defer func() {- sub.Cancel()- logger.Infof("[PubSub] Exited subscription background loop for topic: %s", topicName)- }()- for {- msg, err := sub.Next(context.Background())- if err != nil {- logger.Errorf("[PubSub] subscription Next() error for topic %s: %v", topicName, err)- return- }- logger.Debugf("[PubSub] Received message on topic %s from %s: %s", topicName, msg.ReceivedFrom, string(msg.Data))- n.mu.Lock()- n.receivedMsgs[topicName] = append(n.receivedMsgs[topicName], string(msg.Data))- n.mu.Unlock()- }- }()- return nil+// validateMeshEvent is the GossipSub validator for api.GossipEvents. Reject+// means the message is dropped and not re-forwarded; the signature is checked+// against the trusted control-plane keys, so any peer whose libp2p key+// signed the pubsub envelope still cannot get an unsigned event past here. A+// stale event is ignored rather than rejected: it may be a genuine event+// that arrived late, and there is no reason to penalize its forwarder.+func (n *SamNode) validateMeshEvent(_ context.Context, from peer.ID, msg *pubsub.Message) pubsub.ValidationResult {+ var event api.MeshEvent+ if err := proto.Unmarshal(msg.Data, &event); err != nil {+ logger.Warnw("[Mesh Event] rejecting undecodable event", "event", meshEventSpoofingAttempt, "peer", from.String())+ return pubsub.ValidationReject+ }+ if !n.verifyEvent(&event) {+ logger.Warnw("[Mesh Event] potential spoofing attempt: invalid event signature", "event", meshEventSpoofingAttempt, "peer", from.String())+ return pubsub.ValidationReject+ }+ eventTime := time.UnixMilli(event.Timestamp)+ if time.Since(eventTime) > FreshnessThreshold || time.Until(eventTime) > FreshnessThreshold {+ logger.Warnw("[Mesh Event] dropping stale or future event", "event", meshEventStaleEvent, "peer", from.String(), "timestamp", event.Timestamp)+ return pubsub.ValidationIgnore+ }+ return pubsub.ValidationAccept}func (n *SamNode) startDiscovery(ctx context.Context, meshID string, interval time.Duration) {diff --git a/internal/node/openai_facade.go b/internal/node/openai_facade.goindex dee7b216..4ebcd37d 100644--- a/internal/node/openai_facade.go+++ b/internal/node/openai_facade.go@@ -408,33 +408,31 @@ func (f *openAIFacade) handleCompletions(w http.ResponseWriter, r *http.Request)if p.peerID == "" {f.serveLocal(aw, attempt, p.service)} else {- // Labels ranked this provider; the label gate is the enforcement- // point: the provider's biscuit must attest the requirement before- // the request body leaves this node. The gate also runs when only- // the operator's floor requires it — a caller that asked for- // nothing is still held to the floor (VerifyPeerLabels ANDs both).- if len(requiredLabels) > 0 || len(f.floor()) > 0 {- if f.verifyPeerLabels == nil {+ // Every provider is verified before the request body leaves this+ // node: a discovered peer is only a provider once it has shown a+ // control-plane-signed biscuit bound to it. The caller's label+ // requirement and the operator's floor are extra checks on that+ // same token (VerifyPeerLabels ANDs both).+ if f.verifyPeerLabels == nil {+ recordFacadeRejection(reasonLabelUnattested)+ writeOpenAIError(w, http.StatusServiceUnavailable, "label_unattested",+ "provider verification is unavailable on this node")+ return+ }+ // No backoff on failure: the verdict is requirement-scoped,+ // the provider stays eligible for unconstrained requests.+ if err := f.verifyPeerLabels(r.Context(), p.peerID, requiredLabels); err != nil {+ // With no caller requirement what failed is the floor or the+ // peer's own identity; attribute it so the operator can tell+ // their floor apart from a caller's requirement.+ if len(requiredLabels) == 0 {+ recordFacadeRejection(reasonEgressFloorMismatch)+ } else {recordFacadeRejection(reasonLabelUnattested)- writeOpenAIError(w, http.StatusServiceUnavailable, "label_unattested",- "label enforcement is unavailable on this node")- return- }- // No backoff on failure: the verdict is requirement-scoped,- // the provider stays eligible for unconstrained requests.- if err := f.verifyPeerLabels(r.Context(), p.peerID, requiredLabels); err != nil {- // With no caller requirement the only thing that can have- // failed is the floor; attribute it so the operator can- // tell their floor apart from a caller's requirement.- if len(requiredLabels) == 0 {- recordFacadeRejection(reasonEgressFloorMismatch)- } else {- recordFacadeRejection(reasonLabelUnattested)- }- logger.Warnf("[OpenAIFacade] provider peer=%q service=%q failed label attestation for %v (egress floor %v): %v; trying next",- p.peerID, p.service, requiredLabels, f.floor(), err)- continue}+ logger.Warnf("[OpenAIFacade] provider peer=%q service=%q failed verification for %v (egress floor %v): %v; trying next",+ p.peerID, p.service, requiredLabels, f.floor(), err)+ continue}attempt.URL.Path = fmt.Sprintf("/sam/%s/%s/%s%s", p.peerID, api.ServiceTypeStringInference, p.service, r.URL.Path)attempt.URL.RawPath = ""diff --git a/internal/node/openai_facade_test.go b/internal/node/openai_facade_test.goindex 7fd8e3fb..45b18366 100644--- a/internal/node/openai_facade_test.go+++ b/internal/node/openai_facade_test.go@@ -50,6 +50,8 @@ func newFakeModelService(name string, handler http.Handler, models ...string) *f}// newTestFacade builds a facade with inert seams; tests override as needed.+// The provider verifier accepts everyone: tests that care about the gate+// replace it, and tests of the fail-closed path set it to nil explicitly.func newTestFacade() *openAIFacade {return &openAIFacade{ttl: time.Minute,@@ -61,6 +63,7 @@ func newTestFacade() *openAIFacade {remoteModels: func(_ context.Context, _, _ string) ([]string, error) {return nil, nil},+ verifyPeerLabels: func(context.Context, string, map[string]string) error { return nil },}}@@ -737,7 +740,8 @@ func TestFacade_Completions_LabelAttestation(t *testing.T) {})t.Run("requirement with enforcement unavailable fails closed", func(t *testing.T) {- f := newLabelFacade() // verifyPeerLabels deliberately nil+ f := newLabelFacade()+ f.verifyPeerLabels = nilf.forward = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {t.Error("request must not be forwarded without attestation")})@@ -752,16 +756,33 @@ func TestFacade_Completions_LabelAttestation(t *testing.T) {}})- t.Run("no requirement never attests", func(t *testing.T) {+ t.Run("no requirement still verifies the provider", func(t *testing.T) {+ // A discovered peer is not a provider until its identity checks+ // out, whether or not the caller asked for a label. Here every+ // provider fails verification, so nothing may be forwarded.f := newLabelFacade()- f.verifyPeerLabels = func(_ context.Context, _ string, _ map[string]string) error {- t.Error("attestation must not run without a requirement")- return nil+ verified := 0+ f.verifyPeerLabels = func(_ context.Context, _ string, required map[string]string) error {+ verified+++ if len(required) != 0 {+ t.Errorf("required = %v, want none", required)+ }+ return fmt.Errorf("not an enrolled peer")}- f.forward = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})+ f.forward = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ t.Error("an unverified provider must not receive the request")+ })req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(`{"model":"m1"}`))- f.handleCompletions(httptest.NewRecorder(), req)+ rec := httptest.NewRecorder()+ f.handleCompletions(rec, req)++ if verified != 2 {+ t.Errorf("verifier ran %d times, want once per provider (2)", verified)+ }+ if rec.Code == http.StatusOK {+ t.Errorf("status = %d, want a failure when no provider verifies", rec.Code)+ }})}@@ -963,7 +984,8 @@ func TestFacade_Completions_FloorGatesSilentCaller(t *testing.T) {})t.Run("a floor with no gate seam fails closed", func(t *testing.T) {- f := newFloorFacade() // verifyPeerLabels stays nil+ f := newFloorFacade()+ f.verifyPeerLabels = nilforwarded := falsef.forward = http.HandlerFunc(func(http.ResponseWriter, *http.Request) { forwarded = true })diff --git a/internal/node/proxy_test.go b/internal/node/proxy_test.goindex c9cc5c35..c932aec5 100644--- a/internal/node/proxy_test.go+++ b/internal/node/proxy_test.go@@ -123,17 +123,9 @@ func TestDatapathIntegration(t *testing.T) {}}()- // Setup identity for Node B (the caller proxy) and trust it on Node A- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root: %v", err)- }- if err := buildAndSaveBiscuit(nodeB, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeA.keysMu.Lock()- nodeA.trustedKeys = append(nodeA.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeA.keysMu.Unlock()+ // Both nodes enrolled under one root: B (the caller proxy) authenticates+ // to A, and A must show B a verifiable identity before B forwards to it.+ enrollUnderRoot(t, nodeA, nodeB)// Connect Node B to Node A directlyerr = nodeB.Host.Connect(ctx, peer.AddrInfo{ID: nodeA.Host.ID(), Addrs: nodeA.Host.Addrs()})@@ -468,17 +460,8 @@ func TestStdioDatapathIntegration(t *testing.T) {}defer func() { _ = nodeB.Host.Close() }()- // Setup identity for Node B (the caller proxy) and trust it on Node A- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root: %v", err)- }- if err := buildAndSaveBiscuit(nodeB, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeA.keysMu.Lock()- nodeA.trustedKeys = append(nodeA.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeA.keysMu.Unlock()+ // Both nodes enrolled under one root (see TestDatapathIntegration).+ enrollUnderRoot(t, nodeA, nodeB)// Connect Node B to Node A directlyerr = nodeB.Host.Connect(ctx, peer.AddrInfo{ID: nodeA.Host.ID(), Addrs: nodeA.Host.Addrs()})@@ -656,16 +639,7 @@ func TestDatapathHeadersAndRoutingTable(t *testing.T) {}}()- rootPub, rootPriv, err := ed25519.GenerateKey(rand.Reader)- if err != nil {- t.Fatalf("gen root: %v", err)- }- if err := buildAndSaveBiscuit(nodeB, rootPriv); err != nil {- t.Fatalf("buildAndSaveBiscuit: %v", err)- }- nodeA.keysMu.Lock()- nodeA.trustedKeys = append(nodeA.trustedKeys, TrustedKey{Key: rootPub, ReceivedAt: time.Now()})- nodeA.keysMu.Unlock()+ enrollUnderRoot(t, nodeA, nodeB)err = nodeB.Host.Connect(ctx, peer.AddrInfo{ID: nodeA.Host.ID(), Addrs: nodeA.Host.Addrs()})if err != nil {diff --git a/internal/node/relay_acl_test.go b/internal/node/relay_acl_test.goindex c0829300..94d86574 100644--- a/internal/node/relay_acl_test.go+++ b/internal/node/relay_acl_test.go@@ -43,11 +43,18 @@ func TestNodeRelayACL_AllowConnect(t *testing.T) {t.Errorf("Expected AllowConnect to return false when dest is not authenticated, even if src is")}- // Dest is authenticated, src is not -> should succeed+ // Dest is authenticated, src is not -> should fail: an unauthenticated+ // source must not reach admitted peers through this node's relay.node.authPeers.Delete(srcPeer)node.authPeers.Store(destPeer, time.Now().Add(time.Hour))+ if acl.AllowConnect(srcPeer, srcAddr, destPeer) {+ t.Errorf("Expected AllowConnect to return false when src is not authenticated, even if dest is")+ }++ // Both authenticated -> should succeed+ node.authPeers.Store(srcPeer, time.Now().Add(time.Hour))if !acl.AllowConnect(srcPeer, srcAddr, destPeer) {- t.Errorf("Expected AllowConnect to return true when dest is authenticated")+ t.Errorf("Expected AllowConnect to return true when both src and dest are authenticated")}}diff --git a/internal/node/sidecar.go b/internal/node/sidecar.goindex 38ee0af3..ff3c5cac 100644--- a/internal/node/sidecar.go+++ b/internal/node/sidecar.go@@ -747,37 +747,40 @@ func createEgressProxy(node *SamNode) http.Handler {return}- // The operator's egress floor (egress.require_labels) holds at this- // chokepoint whatever the surface, so an agent that skips the facade- // and dials /sam/<peer>/... raw is gated the same way. required is nil:- // any caller requirement was already enforced by the surface that- // parsed it; the floor is what a silent caller cannot waive.- if floor := node.egressFloor(); len(floor) > 0 {- route, ok := parseEgressRoute(r.URL.Path)- if !ok {- http.Error(w, "Forbidden: egress floor in force and request names no peer", http.StatusForbidden)- return- }- pid, err := peer.Decode(route.peerID)- if err != nil {- http.Error(w, "Bad Request: invalid peer ID", http.StatusBadRequest)- return- }- // The verdict is for the canonical peer, so the dial must name the- // same form: rewrite the segment the Director will re-parse rather- // than let a non-canonical spelling travel past the gate.- if canonical := pid.String(); route.peerID != canonical {- parts := strings.SplitN(r.URL.Path, "/", 6)- if len(parts) >= 3 {- parts[2] = canonical- r.URL.Path = strings.Join(parts, "/")- }+ // Every egress destination must prove it is an enrolled peer: the+ // label gate verifies the peer's control-plane-signed biscuit (cached+ // per peer) and, when the operator set an egress floor+ // (egress.require_labels), holds it to that too. required is nil: any+ // caller requirement was already enforced by the surface that parsed+ // it; the floor is what a silent caller cannot waive.+ route, ok := parseEgressRoute(r.URL.Path)+ if !ok {+ http.Error(w, "Bad Request: request names no peer", http.StatusBadRequest)+ return+ }+ pid, err := peer.Decode(route.peerID)+ if err != nil {+ http.Error(w, "Bad Request: invalid peer ID", http.StatusBadRequest)+ return+ }+ // The verdict is for the canonical peer, so the dial must name the+ // same form: rewrite the segment the Director will re-parse rather+ // than let a non-canonical spelling travel past the gate.+ if canonical := pid.String(); route.peerID != canonical {+ parts := strings.SplitN(r.URL.Path, "/", 6)+ if len(parts) >= 3 {+ parts[2] = canonical+ r.URL.Path = strings.Join(parts, "/")}- if err := node.VerifyPeerLabels(r.Context(), pid, nil); err != nil {- logger.Warnf("[Egress] floor gate refused egress to %s: %v", pid, err)+ }+ if err := node.VerifyPeerLabels(r.Context(), pid, nil); err != nil {+ logger.Warnf("[Egress] refused egress to %s: %v", pid, err)+ if len(node.egressFloor()) > 0 {http.Error(w, "Forbidden: provider does not attest the egress floor", http.StatusForbidden)- return+ } else {+ http.Error(w, "Forbidden: destination is not an enrolled peer", http.StatusForbidden)}+ return}r.Header.Set(api.HeaderSamBiscuit, base64.StdEncoding.EncodeToString(biscuitBytes))diff --git a/internal/router/relay_acl_test.go b/internal/router/relay_acl_test.gonew file mode 100644index 00000000..ac6bc2c4--- /dev/null+++ b/internal/router/relay_acl_test.go@@ -0,0 +1,123 @@+// 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 router++import (+ "context"+ "crypto/ed25519"+ "crypto/rand"+ "testing"+ "time"++ "github.com/google/sam/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"+ "github.com/multiformats/go-multiaddr"+ "google.golang.org/protobuf/proto"+)++func newTestPeerID(t *testing.T) peer.ID {+ t.Helper()+ priv, _, err := crypto.GenerateEd25519Key(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ id, err := peer.IDFromPrivateKey(priv)+ if err != nil {+ t.Fatal(err)+ }+ return id+}++// A relay circuit needs both ends authenticated. Every node authenticates to+// the router on connect, so a source that has not is not a mesh member and+// must not reach admitted peers through the router.+func TestRelayACLAllowConnectRequiresAuthenticatedSource(t *testing.T) {+ r := &Router{}+ acl := &relayACL{r: r}+ src, dest := newTestPeerID(t), newTestPeerID(t)+ addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")++ r.authenticatedPeers.Store(dest, true)+ if acl.AllowConnect(src, addr, dest) {+ t.Error("an unauthenticated source must not connect to an authenticated destination")+ }++ r.authenticatedPeers.Store(src, true)+ if !acl.AllowConnect(src, addr, dest) {+ t.Error("two authenticated peers must be allowed to connect")+ }++ r.authenticatedPeers.Delete(dest)+ if acl.AllowConnect(src, addr, dest) {+ t.Error("an authenticated source must not connect to an unauthenticated destination")+ }++ r.authenticatedPeers.Store(dest, true)+ r.bannedPeers.Store(src, time.Now())+ if acl.AllowConnect(src, addr, dest) {+ t.Error("a banned source must not connect even while still marked authenticated")+ }+}++// The router's GossipSub validator drops unsigned or forged events at the+// first hop instead of fanning them out to every attached node.+func TestRouterValidateMeshEvent(t *testing.T) {+ cpPub, cpPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ _, otherPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ r := &Router{trustedPublicKeys: []ed25519.PublicKey{cpPub}}+ from, target := newTestPeerID(t), newTestPeerID(t)++ signed := func(key ed25519.PrivateKey, at time.Time) []byte {+ event := &api.MeshEvent{Type: api.MeshEvent_BANNED, PeerId: target.String(), Timestamp: at.UnixMilli()}+ unsigned, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)+ if err != nil {+ t.Fatal(err)+ }+ event.Signature = ed25519.Sign(key, unsigned)+ data, err := proto.MarshalOptions{Deterministic: true}.Marshal(event)+ if err != nil {+ t.Fatal(err)+ }+ return data+ }++ tests := []struct {+ name string+ data []byte+ want pubsub.ValidationResult+ }{+ {"fresh event from trusted control plane", signed(cpPriv, time.Now()), pubsub.ValidationAccept},+ {"event signed by an untrusted key", signed(otherPriv, time.Now()), pubsub.ValidationReject},+ {"undecodable payload", []byte("junk"), pubsub.ValidationReject},+ {"stale event", signed(cpPriv, time.Now().Add(-2*meshEventFreshness)), pubsub.ValidationIgnore},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ msg := &pubsub.Message{Message: &pubsub_pb.Message{From: []byte(from), Data: tt.data}}+ if got := r.validateMeshEvent(context.Background(), from, msg); got != tt.want {+ t.Errorf("validateMeshEvent = %v, want %v", got, tt.want)+ }+ })+ }+}diff --git a/internal/router/router.go b/internal/router/router.goindex 74a4d34a..85c80a43 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -96,6 +96,12 @@ func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest plogger.Debugf("[Relay] Rejecting connect from %s to %s: dest is banned", src, dest)return false}+ // Both ends must have authenticated: every node authenticates to the+ // router on connect, so a source that has not is not a mesh member.+ if _, ok := a.r.authenticatedPeers.Load(src); !ok {+ logger.Debugf("[Relay] Rejecting connect from %s to %s: src not authenticated", src, dest)+ return false+ }_, ok := a.r.authenticatedPeers.Load(dest)if !ok {logger.Debugf("[Relay] Rejecting connect from %s to %s: dest not authenticated", src, dest)@@ -340,13 +346,21 @@ func (r *Router) Start() error {return err}- // Setup PubSub- ps, err := pubsub.NewGossipSub(r.ctx, hostNode)+ // Setup PubSub. StrictSign is the default; pinned because the event+ // validator trusts msg.GetFrom().+ ps, err := pubsub.NewGossipSub(r.ctx, hostNode, pubsub.WithMessageSignaturePolicy(pubsub.StrictSign))if err != nil {_ = hostNode.Close()return err}r.PubSub = ps+ // The router is the hub every node gossips through. Validating here+ // stops a junk event at the first hop instead of fanning it out to every+ // attached node, each of which would spend a verify on it.+ if err := ps.RegisterTopicValidator(api.GossipEvents, r.validateMeshEvent); err != nil {+ _ = hostNode.Close()+ return fmt.Errorf("register mesh event validator: %w", err)+ }topic, err := ps.Join(api.GossipEvents)if err != nil {@@ -714,6 +728,31 @@ func (r *Router) verifyEvent(event *api.MeshEvent) bool {return false}+// meshEventFreshness bounds how far an event's timestamp may be from now+// before it is treated as a replay (or a clock the router cannot trust).+const meshEventFreshness = 5 * time.Minute++// validateMeshEvent is the GossipSub validator for api.GossipEvents: reject+// (drop and do not forward) anything undecodable or not signed by a trusted+// control plane, ignore anything stale.+func (r *Router) validateMeshEvent(_ context.Context, from peer.ID, msg *pubsub.Message) pubsub.ValidationResult {+ var event api.MeshEvent+ if err := proto.Unmarshal(msg.Data, &event); err != nil {+ logger.Warnf("[Router Event] Rejecting undecodable event from %s", from)+ return pubsub.ValidationReject+ }+ if !r.verifyEvent(&event) {+ logger.Warnf("[Router Event] Potential spoofing attempt: invalid signature on event from %s", from)+ return pubsub.ValidationReject+ }+ eventTime := time.UnixMilli(event.Timestamp)+ if time.Since(eventTime) > meshEventFreshness || time.Until(eventTime) > meshEventFreshness {+ logger.Warnf("[Router Event] Dropping stale or future event from %s", from)+ return pubsub.ValidationIgnore+ }+ return pubsub.ValidationAccept+}+func (r *Router) listenForControlPlaneEvents(ctx context.Context) {defer r.wg.Done()if r.EventTopic == nil {@@ -732,23 +771,13 @@ func (r *Router) listenForControlPlaneEvents(ctx context.Context) {return}+ // validateMeshEvent already rejected anything unsigned or stale.var event api.MeshEventif err := proto.Unmarshal(msg.Data, &event); err != nil {logger.Errorf("[Router Event] Failed to unmarshal event from %s: %v", msg.ReceivedFrom, err)continue}- if !r.verifyEvent(&event) {- logger.Warnf("[Router Event] Potential spoofing attempt: invalid signature on event from %s", msg.ReceivedFrom)- continue- }-- eventTime := time.UnixMilli(event.Timestamp)- if time.Since(eventTime) > 5*time.Minute || time.Until(eventTime) > 5*time.Minute {- logger.Warnf("[Router Event] Dropping stale or future event from %s", msg.ReceivedFrom)- continue- }-switch event.Type {case api.MeshEvent_BANNED:if event.PeerId != "" {diff --git a/site/content/docs/integrations/vscode-copilot.md b/site/content/docs/integrations/vscode-copilot.mdindex 07eb81f7..aad1d6f6 100644--- a/site/content/docs/integrations/vscode-copilot.md+++ b/site/content/docs/integrations/vscode-copilot.md@@ -338,10 +338,9 @@ The real output:```textmesh offered model: openrouter/auto-mesh granted 9 tools: call_remote_tool, describe_remote_tool,+mesh granted 6 tools: call_remote_tool, describe_remote_tool,discover_remote_services, find_remote_tools, get_mesh_info,- list_local_services, mesh_pubsub_broadcast, poll_messages,- subscribe_topic+ list_local_servicesstep 1: get_mesh_info({})This node is connected to **23 peers**.```diff --git a/site/content/docs/snippets/banana_bot_playground.py b/site/content/docs/snippets/banana_bot_playground.pydeleted file mode 100644index 35761abe..00000000--- a/site/content/docs/snippets/banana_bot_playground.py+++ /dev/null@@ -1,441 +0,0 @@-import asyncio-import os-import sys-import json-import random-import httpx-from typing import Optional, Dict, Any, List-from mcp import ClientSession-from mcp.client.streamable_http import streamable_http_client--class SamClient:- """Inlined SAM Client for self-contained execution."""- def __init__(self, server_url: Optional[str] = None, token: Optional[str] = None):- if server_url is None:- server_url = os.environ.get("SAM_MCP_URL", "http://localhost:8080/mcp")- if token is None:- token = os.environ.get("SAM_API_TOKEN")- self.server_url = server_url- self.token = token- self.session: Optional[ClientSession] = None- self._sse_cm = None- self.lock = asyncio.Lock()-- async def connect(self):- headers = {"Accept": "application/json, text/event-stream"}- if self.token:- headers["Authorization"] = f"Bearer {self.token}"- self._http_client = httpx.AsyncClient(- headers=headers,- follow_redirects=True,- # The SDK's SSE-friendly defaults; httpx's default 5s read timeout drops the stream.- timeout=httpx.Timeout(30.0, read=300.0),- )- try:- self._sse_cm = streamable_http_client(self.server_url, http_client=self._http_client)- res = await self._sse_cm.__aenter__()- read_stream, write_stream = res[0], res[1]- self.session = ClientSession(read_stream, write_stream)- await self.session.__aenter__()- await self.session.initialize()- except Exception:- # The retry loop in __aenter__ would otherwise orphan this- # attempt's http client and half-entered streams.- await self.close()- raise-- async def close(self):- if self.session:- await self.session.__aexit__(None, None, None)- if self._sse_cm:- await self._sse_cm.__aexit__(None, None, None)- if getattr(self, "_http_client", None):- await self._http_client.aclose()- self.session = None- self._sse_cm = None- self._http_client = None-- async def get_tools(self) -> List[Dict[str, Any]]:- if not self.session:- raise RuntimeError("Not connected")- async with self.lock:- resp = await self.session.list_tools()- return [t.model_dump() if hasattr(t, "model_dump") else t for t in resp.tools]-- async def call_tool(self, name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:- if not self.session:- raise RuntimeError("Not connected")- async with self.lock:- resp = await self.session.call_tool(name, arguments)- return resp.model_dump() if hasattr(resp, "model_dump") else resp-- async def __aenter__(self):- for attempt in range(1, 13):- try:- await self.connect()- return self- except Exception as e:- if attempt == 12:- raise- print(f"[-] Failed to connect to SAM node (attempt {attempt}/12): {e}. Retrying in 5 seconds...")- await asyncio.sleep(5.0)- return self-- async def __aexit__(self, exc_type, exc_val, exc_tb):- await self.close()--CHAT_TOPIC = "mesh-chat-playground"-MODEL_NAME = "gemini-flash-latest"-NEW_CHATS_BUFFER = []--SYSTEM_INSTRUCTION = (- "You are the Banana Bot (also known as the Mesh Police), a friendly, banana-themed AI bot "- "controlling and monitoring the Sovereign Agent Mesh (SAM). Your job is to explore the mesh, "- "inspect remote peers, verify they are healthy by executing their tools, and post "- "playful investigation reports, comments, or banana jokes to the public GossipSub chat topic. "- "Always stay in character. Use plenty of banana emojis (🍌, 💛, 🐒) and make jokes about "- "peers or users. You must act autonomously based on the tools available."-)--def to_gemini_schema(schema: dict) -> dict:- """Helper to convert JSON schema types (lowercase) to Gemini API schema types (uppercase) and strip unsupported fields."""- if not isinstance(schema, dict):- return schema- res = {}- for k, v in schema.items():- if k == "additionalProperties":- continue- if k == "type" and isinstance(v, str):- res[k] = v.upper()- elif isinstance(v, dict):- res[k] = to_gemini_schema(v)- elif isinstance(v, list):- res[k] = [to_gemini_schema(item) if isinstance(item, dict) else item for item in v]- else:- res[k] = v- return res--class FallbackAgent:- def __init__(self, client: SamClient, api_key: str, token: str, local_node_url: str):- self.client = client- self.api_key = api_key- self.token = token- self.base_url = local_node_url.rsplit("/mcp", 1)[0]- self.current_model = None- self.mesh_history = []- self.gemini_history = []-- def clear_session(self):- """Reset conversational history at the end of each cycle."""- self.current_model = None- self.mesh_history = []- self.gemini_history = []-- async def get_vllm_peer_id(self) -> str:- """Finds the peer ID of the vllm-tpu service on the mesh."""- try:- res = await self.client.call_tool("discover_remote_services", {"type": "inference", "name": "vllm-tpu"})- content = res.get("content", [{}])[0].get("text", "[]")- peers = json.loads(content)- if peers:- return peers[0].get("peer_id")- except Exception as e:- pass- return ""-- async def step(self, prompt: str, mcp_tools: list) -> dict:- if self.current_model is None:- peer_id = await self.get_vllm_peer_id()- if peer_id:- print(f"[*] [Fallback Agent] Found vllm-tpu on peer {peer_id[:8]}... Trying mesh model.")- self.current_model = "mesh"- res = await self.step_mesh(peer_id, prompt, mcp_tools)- if res:- return res- print("[*] [Fallback Agent] Mesh model failed. Falling back to Gemini.")-- self.current_model = "gemini"-- if self.current_model == "mesh":- peer_id = await self.get_vllm_peer_id()- if peer_id:- res = await self.step_mesh(peer_id, prompt, mcp_tools)- if res:- return res- print("[*] [Fallback Agent] Mesh model follow-up failed. Falling back to Gemini.")- self.current_model = "gemini"- return await self.step_gemini(prompt, mcp_tools)- else:- return await self.step_gemini(prompt, mcp_tools)-- async def step_mesh(self, peer_id: str, prompt: str, mcp_tools: list) -> dict:- url = f"{self.base_url}/sam/{peer_id}/inference/vllm-tpu/v1/chat/completions"- headers = {- "Authorization": f"Bearer {self.token}",- "Content-Type": "application/json"- }-- tools = []- for tool in mcp_tools:- tools.append({- "type": "function",- "function": {- "name": tool["name"],- "description": tool.get("description", ""),- "parameters": tool.get("input_schema") or tool.get("inputSchema") or {}- }- })-- messages = list(self.mesh_history)- if not messages or messages[-1].get("role") != "user":- messages.append({"role": "user", "content": prompt})- self.mesh_history.append({"role": "user", "content": prompt})-- payload = {- "model": "google/gemma-2-2b-it",- "messages": messages,- "temperature": 0.2- }- if tools:- payload["tools"] = tools-- try:- async with httpx.AsyncClient() as http_client:- resp = await http_client.post(url, json=payload, headers=headers, timeout=25.0)- if resp.status_code != 200:- print(f"[-] Mesh Model HTTP Error {resp.status_code}: {resp.text}")- return None-- data = resp.json()- choice = data.get("choices", [{}])[0]- message = choice.get("message", {})-- text_response = message.get("content") or ""- tool_calls = message.get("tool_calls") or []-- calls = []- for tc in tool_calls:- fn = tc.get("function", {})- args = {}- if fn.get("arguments"):- try:- args = json.loads(fn["arguments"])- except Exception:- pass- calls.append({- "name": fn.get("name"),- "args": args- })-- self.mesh_history.append(message)- return {- "text": text_response,- "calls": calls- }- except Exception as e:- return None-- async def step_gemini(self, prompt: str, mcp_tools: list) -> dict:- url = f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL_NAME}:generateContent?key={self.api_key}"-- function_declarations = []- for tool in mcp_tools:- decl = {- "name": tool["name"],- "description": tool.get("description", ""),- }- if "input_schema" in tool:- decl["parameters"] = to_gemini_schema(tool["input_schema"])- elif "inputSchema" in tool:- decl["parameters"] = to_gemini_schema(tool["inputSchema"])- function_declarations.append(decl)-- contents = list(self.gemini_history)- contents.append({"role": "user", "parts": [{"text": prompt}]})- self.gemini_history.append({"role": "user", "parts": [{"text": prompt}]})-- payload = {- "contents": contents,- "systemInstruction": {- "parts": [{"text": SYSTEM_INSTRUCTION}]- }- }- if function_declarations:- payload["tools"] = [{"functionDeclarations": function_declarations}]-- for attempt in range(3):- try:- async with httpx.AsyncClient() as http_client:- resp = await http_client.post(url, json=payload, timeout=25.0)- if resp.status_code == 200:- data = resp.json()- candidates = data.get("candidates", [])- if not candidates:- return {"text": "No response from Gemini.", "calls": []}-- candidate = candidates[0]- content = candidate.get("content", {})- parts = content.get("parts", [])-- text_response = ""- calls = []- for part in parts:- if "text" in part:- text_response += part["text"]- if "functionCall" in part:- calls.append(part["functionCall"])-- self.gemini_history.append(content)- return {- "text": text_response,- "calls": calls- }- elif resp.status_code in [429, 503]:- print(f"[-] Gemini returned {resp.status_code}, retrying in 3s (attempt {attempt+1}/3)...")- await asyncio.sleep(3.0)- continue- else:- print(f"[-] Gemini HTTP Error {resp.status_code}: {resp.text}")- return {"text": f"Gemini error {resp.status_code}.", "calls": []}- except Exception as e:- if attempt == 2:- return {"text": f"Gemini error: {e}", "calls": []}- print(f"[-] Gemini connection exception: {e}, retrying in 3s...")- await asyncio.sleep(3.0)- return {"text": "Gemini error: Max retries exceeded.", "calls": []}-- def add_tool_response(self, function_name: str, response_data: dict):- if self.current_model == "mesh":- self.mesh_history.append({- "role": "tool",- "name": function_name,- "content": json.dumps(response_data),- "tool_call_id": "call_1"- })- else:- self.gemini_history.append({- "role": "function",- "parts": [{- "functionResponse": {- "name": function_name,- "response": response_data- }- }]- })--async def poll_chat_messages(client: SamClient):- """Listens for GossipSub chat messages on the mesh and displays them."""- print(f"[*] Subscribing to GossipSub chat: '{CHAT_TOPIC}'")- await client.call_tool("subscribe_topic", {"topic": CHAT_TOPIC})-- print("[*] Listening for mesh chat messages...")- while True:- try:- res = await client.call_tool("poll_messages", {"topic": CHAT_TOPIC})- text = res.get("content", [{}])[0].get("text", "")- if "Messages on topic" in text and "[]" not in text:- print(f"\n📢 [Mesh Chat Channel] {text.strip()}")- prefix = f"Messages on topic {CHAT_TOPIC}: "- raw_msgs = text[len(prefix):].strip()- NEW_CHATS_BUFFER.append(raw_msgs)- if len(NEW_CHATS_BUFFER) > 10:- NEW_CHATS_BUFFER.pop(0)- except Exception as e:- print(f"[-] poll_messages error: {e}")- await asyncio.sleep(2.0)- await asyncio.sleep(2.0)--async def run_banana_bot():- api_key = os.environ.get("GEMINI_API_KEY")- url = os.environ.get("SAM_MCP_URL", "http://127.0.0.1:8080/mcp")- token = os.environ.get("SAM_API_TOKEN", "secret-token")-- if not api_key:- print("[!] GEMINI_API_KEY environment variable is not set. The bot will rely solely on the mesh model.")-- print("=" * 70)- print(" 🍌 BANANA BOT (MESH POLICE) ACTIVE ON SOVEREIGN AGENT MESH 🍌")- print("=" * 70)- print(f"Connecting to local node: {url}")-- try:- async with SamClient(server_url=url, token=token) as client:- print("[+] Connected to SAM local node.")-- agent = FallbackAgent(client, api_key=api_key or "", token=token, local_node_url=url)-- # Start background task to listen to public GossipSub channel- asyncio.create_task(poll_chat_messages(client))-- # Introduce ourselves to the mesh chat- intro_msg = "🍌 [Banana Bot] Hello mesh! The Mesh Police is now online and patrolling. Watch out for slips! 🍌"- await client.call_tool("mesh_pubsub_broadcast", {"topic": CHAT_TOPIC, "payload": intro_msg})-- while True:- print("\n[*] Banana Bot scanning mesh for active peers...")-- # Fetch local control plane tools- local_tools = await client.get_tools()-- # Discover remote services- disc_res = await client.call_tool("discover_remote_services", {"type": "mcp"})- disc_text = disc_res.get("content", [{}])[0].get("text", "[]")- try:- peers = json.loads(disc_text)- except Exception:- peers = []-- # Extract and clear chats buffer- chats_snapshot = list(NEW_CHATS_BUFFER)- NEW_CHATS_BUFFER.clear()-- mesh_status = (- f"Current active remote peers in the mesh: {json.dumps(peers)}\n"- f"Incoming chat messages on GKE GossipSub topic '{CHAT_TOPIC}' since last scan: {json.dumps(chats_snapshot)}\n\n"- "Select a peer to inspect, fetch its tools, run a tool, or broadcast "- "a reply/reaction to the incoming chat messages to the chat channel."- )-- print(f"[*] Discovered {len(peers)} remote peer(s). Prompting Banana Bot...")-- # Step Fallback Agent- result = await agent.step(mesh_status, local_tools)-- if result["text"]:- print(f"\n🧠 [Banana Bot Thoughts]: {result['text'].strip()}")-- # Execute any function calls requested by the agent- for call in result["calls"]:- func_name = call["name"]- func_args = call.get("args", {})-- print(f"\n⚡ [Banana Bot Action] Executing local tool '{func_name}' with args: {func_args}")- try:- tool_res = await client.call_tool(func_name, func_args)- print(f"🟢 [Banana Bot Action] Success: {tool_res}")-- agent.add_tool_response(func_name, tool_res)-- follow_up = await agent.step("Process the tool response and report/act.", local_tools)- if follow_up["text"]:- print(f"\n🧠 [Banana Bot Follow-up]: {follow_up['text'].strip()}")- except Exception as err:- print(f"🔴 [Banana Bot Action] Failed: {err}")- agent.add_tool_response(func_name, {"error": str(err)})-- # Clear session history at the end of this patrol cycle- agent.clear_session()-- await asyncio.sleep(20.0)-- except KeyboardInterrupt:- print("\n[-] Banana Bot shutting down. Off duty! 🍌")- except Exception as e:- print(f"[!] Banana Bot crashed: {e}")- sys.exit(1)--if __name__ == "__main__":- try:- asyncio.run(run_banana_bot())- except KeyboardInterrupt:- print("\n[-] Off duty!")diff --git a/site/content/docs/sovereignty.md b/site/content/docs/sovereignty.mdindex 2b9cd1c6..0eb28f82 100644--- a/site/content/docs/sovereignty.md+++ b/site/content/docs/sovereignty.md@@ -135,5 +135,5 @@ When deploying SAM for mission-critical, sovereign agent operations:2. **Maintain Root Cryptographic Key Custody:** Generate, manage, and hold your own Ed25519 root signing keys (via KMS/Cloud EKM or HSMs).3. **Use Your Own OIDC Identity Provider:** Point `--issuer` to your internal Keycloak, Dex, or corporate IdP.4. **Declare & Attest Sovereignty Labels:** Declare `labels: {jurisdiction: eu, region: <your-region>}` in each node's `sam-node.yaml` and configure control plane roles with `allowed_labels`.-5. **Enforce Jurisdictional Egress:** Set `egress.require_labels` in `sam-node.yaml` (e.g. `jurisdiction: eu`) so every provider must attest the boundary before the node sends it anything. Agents may narrow further per request with `X-Sam-Required-Labels`, but cannot widen past the floor or waive it by omitting the header — the two are checked independently and every pair of the floor must hold.+5. **Enforce Jurisdictional Egress:** Set `egress.require_labels` in `sam-node.yaml` (e.g. `jurisdiction: eu`) so every provider must attest the boundary before the node sends it anything. Agents may narrow further per request with `X-Sam-Required-Labels`, but cannot widen past the floor or waive it by omitting the header — the two are checked independently and every pair of the floor must hold. Independently of any floor, a node forwards nothing to a peer that has not shown a control-plane-signed identity bound to it; discovery names candidates, it does not vouch for them.6. **Set Local Attenuation Vetoes:** Configure local node `attenuation.policies` to retain final destination-side access control.diff --git a/tests/integration/failover_test.go b/tests/integration/failover_test.goindex e04f9b47..6b05ea6d 100644--- a/tests/integration/failover_test.go+++ b/tests/integration/failover_test.go@@ -278,21 +278,35 @@ roles: []if err != nil {t.Fatal(err)}+ routerInfoB, err := peer.AddrInfoFromP2pAddr(multiaddr.StringCast(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", routerPortB, peerIDB)))+ if err != nil {+ t.Fatal(err)+ }- ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)defer cancel()+ // The relay admits circuits only between peers that authenticated to+ // the router, so the client is a mesh member: enrolled with CP B and+ // handshaken with Router B before it dials the circuit. A fresh host per+ // attempt sidesteps the swarm's dial backoff.var connectErr errorfor i := 0; i < 15; i++ {clientHost, err := libp2p.New(libp2p.NoListenAddrs, libp2p.EnableRelay())if err != nil {t.Fatal(err)}- connectErr = clientHost.Connect(ctx, *addrInfo)+ clientBiscuit := enrollClientOnControlPlane(t, httpPortCP_B, clientHost.ID(), clientHost.Peerstore().PrivKey(clientHost.ID()), nodeJWT)+ if connectErr = clientHost.Connect(ctx, *routerInfoB); connectErr == nil {+ if connectErr = authenticateWithRouter(ctx, clientHost, routerInfoB.ID, clientBiscuit); connectErr == nil {+ connectErr = clientHost.Connect(ctx, *addrInfo)+ }+ }_ = clientHost.Close()if connectErr == nil {break}+ t.Logf("attempt %d: %v", i+1, connectErr)time.Sleep(1 * time.Second)}@@ -300,6 +314,20 @@ roles: []t.Fatalf("Failed to connect to Node B via router B relay: %v\nOutput: %s", connectErr, stdoutNode.String()+stderrNode.String())}t.Log("Successfully connected to Node B via router B relay circuit!")++ // The same circuit is refused to a host that never authenticated to+ // Router B: the relay is for mesh members, not for whoever can reach+ // the router's port.+ anonHost, err := libp2p.New(libp2p.NoListenAddrs, libp2p.EnableRelay())+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = anonHost.Close() }()+ anonCtx, anonCancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer anonCancel()+ if err := anonHost.Connect(anonCtx, *addrInfo); err == nil {+ t.Fatal("an unauthenticated host reached the node through Router B's relay; the relay ACL must require an authenticated source")+ }}// waitForActiveRouters polls the control plane's /info endpoint until atdiff --git a/tests/integration/multimaster_test.go b/tests/integration/multimaster_test.goindex 0622da45..572d3270 100644--- a/tests/integration/multimaster_test.go+++ b/tests/integration/multimaster_test.go@@ -29,6 +29,7 @@ import ("github.com/google/sam/api""github.com/libp2p/go-libp2p""github.com/libp2p/go-libp2p/core/crypto"+ "github.com/libp2p/go-libp2p/core/host""github.com/libp2p/go-libp2p/core/peer""github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr"@@ -235,44 +236,46 @@ roles: []t.Fatalf("failed to connect client to Router A: %v", err)}- t.Log("Opening auth stream to Router A...")- s, err := clientHost.NewStream(context.Background(), routerInfoA.ID, api.AuthProtocolID)+ t.Log("Authenticating to Router A with the CP B biscuit...")+ if err := authenticateWithRouter(ctxConnect, clientHost, routerInfoA.ID, clientBiscuit); err != nil {+ t.Fatalf("mutual auth with Router A failed: %v\nRouter A Stderr:\n%s\nRouter A Stdout:\n%s\nCP A Stderr:\n%s\nCP B Stderr:\n%s",+ err, stderrRouterA.String(), stdoutRouterA.String(), stderrCP_A.String(), stderrCP_B.String())+ }++ t.Log("Successfully verified multi-master control plane signature trust!")+}++// authenticateWithRouter runs the node side of the auth handshake on+// api.AuthProtocolID, which is what admits h to the router's relay.+func authenticateWithRouter(ctx context.Context, h host.Host, router peer.ID, biscuit []byte) error {+ s, err := h.NewStream(ctx, router, api.AuthProtocolID)if err != nil {- t.Fatalf("failed to open auth stream: %v", err)+ return fmt.Errorf("open auth stream: %w", err)}defer func() { _ = s.Close() }()+ _ = s.SetDeadline(time.Now().Add(5 * time.Second))- t.Log("Writing auth frame with CP B biscuit to Router A...")- writer := msgio.NewVarintWriter(s)- authFrame := &api.AuthFrame{Biscuit: clientBiscuit}- authFrameBytes, err := proto.Marshal(authFrame)+ authFrameBytes, err := proto.Marshal(&api.AuthFrame{Biscuit: biscuit})if err != nil {- t.Fatal(err)+ return err}- if err := writer.WriteMsg(authFrameBytes); err != nil {- t.Fatalf("failed to write auth frame: %v", err)+ if err := msgio.NewVarintWriter(s).WriteMsg(authFrameBytes); err != nil {+ return fmt.Errorf("write auth frame: %w", err)}-- t.Log("Reading auth response from Router A...")reader := msgio.NewVarintReaderSize(s, 1024*64)respMsg, err := reader.ReadMsg()if err != nil {- t.Fatalf("failed to read response from Router A: %v\nRouter A Stderr:\n%s\nRouter A Stdout:\n%s\nCP A Stderr:\n%s\nCP B Stderr:\n%s",- err, stderrRouterA.String(), stdoutRouterA.String(), stderrCP_A.String(), stderrCP_B.String())+ return fmt.Errorf("read auth response: %w", err)}defer reader.ReleaseMsg(respMsg)-var authResp api.AuthResponseif err := proto.Unmarshal(respMsg, &authResp); err != nil {- t.Fatalf("failed to unmarshal response: %v", err)+ return fmt.Errorf("decode auth response: %w", err)}-if !authResp.Success {- t.Fatalf("mutual auth with Router A was rejected: %s\nRouter A Stderr:\n%s\nRouter A Stdout:\n%s\nCP A Stderr:\n%s\nCP B Stderr:\n%s",- authResp.Error, stderrRouterA.String(), stdoutRouterA.String(), stderrCP_A.String(), stderrCP_B.String())+ return fmt.Errorf("router rejected the handshake: %s", authResp.Error)}-- t.Log("Successfully verified multi-master control plane signature trust!")+ return nil}func enrollClientOnControlPlane(t *testing.T, cpPort int, clientID peer.ID, privKey crypto.PrivKey, jwtToken string) []byte {diff --git a/tests/integration/pubsub_test.go b/tests/integration/pubsub_test.godeleted file mode 100644index 7a6db0a7..00000000--- a/tests/integration/pubsub_test.go+++ /dev/null@@ -1,183 +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 integration_test--import (- "context"- "os"- "os/exec"- "path/filepath"- "strings"- "testing"- "time"-)--func TestPubSubTools(t *testing.T) {- nodeBin := buildBinary(t, "./cmd/sam-node")- _, routerAddr := startMockRouter(t)- tmpHome1, err := os.MkdirTemp("", "pubsub-test-1")- if err != nil {- t.Fatal(err)- }- t.Logf("Node 1 logs at: %s/node1.log", tmpHome1)-- tmpHome2, err := os.MkdirTemp("", "pubsub-test-2")- if err != nil {- t.Fatal(err)- }- t.Logf("Node 2 logs at: %s/node2.log", tmpHome2)-- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)- defer cancel()-- // Start Node 1- env1 := append(os.Environ(), "HOME="+tmpHome1, "XDG_CONFIG_HOME="+filepath.Join(tmpHome1, ".config"))- cmd1 := exec.CommandContext(ctx, nodeBin, "run", "--control-plane", routerAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5003/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5004", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms", "--api-token-path", tokenPath(t, "test-token"), "--allow-loopback")- cmd1.Env = env1- logFile1, err := os.Create(filepath.Join(tmpHome1, "node1.log"))- if err != nil {- t.Fatal(err)- }- cmd1.Stdout = logFile1- cmd1.Stderr = logFile1- if err := cmd1.Start(); err != nil {- t.Fatal(err)- }- defer func() { _ = cmd1.Process.Kill(); _ = logFile1.Close() }()-- // Start Node 2- env2 := append(os.Environ(), "HOME="+tmpHome2, "XDG_CONFIG_HOME="+filepath.Join(tmpHome2, ".config"))- cmd2 := exec.CommandContext(ctx, nodeBin, "run", "--control-plane", routerAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5005/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5006", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms", "--api-token-path", tokenPath(t, "test-token"), "--allow-loopback")- cmd2.Env = env2- logFile2, err := os.Create(filepath.Join(tmpHome2, "node2.log"))- if err != nil {- t.Fatal(err)- }- cmd2.Stdout = logFile2- cmd2.Stderr = logFile2- if err := cmd2.Start(); err != nil {- t.Fatal(err)- }- defer func() { _ = cmd2.Process.Kill(); _ = logFile2.Close() }()-- // Helper to wait for MCP addr in log- waitForMCPAddr := func(t *testing.T, logPath string) string {- t.Helper()- deadline := time.Now().Add(5 * time.Second)- for time.Now().Before(deadline) {- data, _ := os.ReadFile(logPath)- lines := strings.Split(string(data), "\n")- for _, line := range lines {- if strings.Contains(line, "Starting MCP server on TCP address ") {- parts := strings.Split(line, "Starting MCP server on TCP address ")- if len(parts) > 1 {- return strings.TrimSpace(parts[1])- }- }- }- time.Sleep(100 * time.Millisecond)- }- t.Fatalf("timeout waiting for MCP addr in log: %s", logPath)- return ""- }-- mcpAddr1 := waitForMCPAddr(t, filepath.Join(tmpHome1, "node1.log"))- mcpAddr2 := waitForMCPAddr(t, filepath.Join(tmpHome2, "node2.log"))-- // Helper to call MCP tool- callTool := func(mcpAddr string, toolName string, params map[string]any) string {- return callMCP(t, mcpAddr, toolName, params)- }-- waitForPeerInfoInLog := func(t *testing.T, logPath string) string {- t.Helper()- deadline := time.Now().Add(5 * time.Second)- for time.Now().Before(deadline) {- data, _ := os.ReadFile(logPath)- lines := strings.Split(string(data), "\n")- var peerID string- var tcpAddr string- for _, line := range lines {- if strings.Contains(line, "PeerID: ") {- parts := strings.Split(line, "PeerID: ")- if len(parts) > 1 {- peerID = strings.TrimSpace(parts[1])- }- }- if strings.Contains(line, "Listening on: ") {- parts := strings.Split(line, " ")- for _, p := range parts {- if strings.Contains(p, "/tcp/") {- tcpAddr = strings.Trim(p, "[]")- }- }- }- }- if peerID != "" && tcpAddr != "" {- return tcpAddr + "/p2p/" + peerID- }- time.Sleep(100 * time.Millisecond)- }- t.Fatalf("timeout waiting for peer info in log: %s", logPath)- return ""- }-- // Force Node 1 to connect to Node 2- addr2 := waitForPeerInfoInLog(t, filepath.Join(tmpHome2, "node2.log"))- t.Logf("Node 2 address: %s", addr2)- connectPeer(t, mcpAddr1, addr2)-- // Node 1 subscribes to topic "test-topic"- subscribeResult1 := callTool(mcpAddr1, "subscribe_topic", map[string]any{- "topic": "test-topic",- })- if !strings.Contains(subscribeResult1, "Subscribed") {- t.Fatalf("Subscribe node 1 failed: %s", subscribeResult1)- }-- // Node 2 subscribes to topic "test-topic"- subscribeResult2 := callTool(mcpAddr2, "subscribe_topic", map[string]any{- "topic": "test-topic",- })- if !strings.Contains(subscribeResult2, "Subscribed") {- t.Fatalf("Subscribe node 2 failed: %s", subscribeResult2)- }-- var pollResult string- deadline := time.Now().Add(10 * time.Second)- for time.Now().Before(deadline) {- // Node 1 broadcasts on topic "test-topic"- broadcastResult := callTool(mcpAddr1, "mesh_pubsub_broadcast", map[string]any{- "topic": "test-topic",- "payload": "hello from node 1",- })- if !strings.Contains(broadcastResult, "Published") {- t.Fatalf("Broadcast failed: %s", broadcastResult)- }-- // Node 2 polls for messages on topic "test-topic"- pollResult = callTool(mcpAddr2, "poll_messages", map[string]any{- "topic": "test-topic",- })- if strings.Contains(pollResult, "hello from node 1") {- break- }- time.Sleep(50 * time.Millisecond)- }-- if !strings.Contains(pollResult, "hello from node 1") {- t.Fatalf("Poll failed, expected message not found: %s", pollResult)- }-}