Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
make agent more resilient
Antonio Ojea85db55010e68
diff --git a/api/sam.pb.go b/api/sam.pb.goindex c13076f4..8e606e06 100644--- a/api/sam.pb.go+++ b/api/sam.pb.go@@ -708,6 +708,7 @@ type HubInfoResponse struct {OidcIssuer string `protobuf:"bytes,1,opt,name=oidc_issuer,json=oidcIssuer,proto3" json:"oidc_issuer,omitempty"`ClientId string `protobuf:"bytes,2,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`Audience string `protobuf:"bytes,3,opt,name=audience,proto3" json:"audience,omitempty"`+ HubAddresses []string `protobuf:"bytes,4,rep,name=hub_addresses,json=hubAddresses,proto3" json:"hub_addresses,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}@@ -763,6 +764,13 @@ func (x *HubInfoResponse) GetAudience() string {return ""}+func (x *HubInfoResponse) GetHubAddresses() []string {+ if x != nil {+ return x.HubAddresses+ }+ return nil+}+var File_api_sam_proto protoreflect.FileDescriptorconst file_api_sam_proto_rawDesc = "" +@@ -814,12 +822,13 @@ const file_api_sam_proto_rawDesc = "" +"\apeer_id\x18\x01 \x01(\tR\x06peerId\x12&\n" +"\x0flocal_proxy_url\x18\x02 \x01(\tR\rlocalProxyUrl\x12\x19\n" +"\bsrv_name\x18\x03 \x01(\tR\asrvName\x12'\n" +- "\x0fsrv_description\x18\x04 \x01(\tR\x0esrvDescription\"k\n" ++ "\x0fsrv_description\x18\x04 \x01(\tR\x0esrvDescription\"\x90\x01\n" +"\x0fHubInfoResponse\x12\x1f\n" +"\voidc_issuer\x18\x01 \x01(\tR\n" +"oidcIssuer\x12\x1b\n" +"\tclient_id\x18\x02 \x01(\tR\bclientId\x12\x1a\n" +- "\baudience\x18\x03 \x01(\tR\baudience*s\n" ++ "\baudience\x18\x03 \x01(\tR\baudience\x12#\n" ++ "\rhub_addresses\x18\x04 \x03(\tR\fhubAddresses*s\n" +"\vServiceType\x12\x1c\n" +"\x18SERVICE_TYPE_UNSPECIFIED\x10\x00\x12\x14\n" +"\x10SERVICE_TYPE_MCP\x10\x01\x12\x1a\n" +diff --git a/api/sam.proto b/api/sam.protoindex ad077a6a..19a89c96 100644--- a/api/sam.proto+++ b/api/sam.proto@@ -91,5 +91,6 @@ message HubInfoResponse {string oidc_issuer = 1;string client_id = 2;string audience = 3;+ repeated string hub_addresses = 4;}diff --git a/cmd/sam-hub/main_test.go b/cmd/sam-hub/main_test.goindex 3d0daa3a..e5ce8b0f 100644--- a/cmd/sam-hub/main_test.go+++ b/cmd/sam-hub/main_test.go@@ -26,6 +26,7 @@ import ("github.com/coreos/go-oidc/v3/oidc""github.com/golang-jwt/jwt/v5""github.com/google/sam/api"+ "github.com/libp2p/go-libp2p""github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/peer""google.golang.org/protobuf/proto"@@ -221,9 +222,20 @@ func TestHandleInfoHTTP(t *testing.T) {oidcIssuer = "http://mock-issuer"+ priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ h, err := libp2p.New(libp2p.Identity(priv), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer h.Close()+hub := &Hub{KeyRing: kr,AllowedAudiences: []string{"test-audience-1", "test-audience-2"},+ Host: h,}handler := handleInfoHTTP(hub)diff --git a/cmd/sam-hub/server.go b/cmd/sam-hub/server.goindex 2efb89db..0082b29b 100644--- a/cmd/sam-hub/server.go+++ b/cmd/sam-hub/server.go@@ -140,9 +140,10 @@ func handleInfoHTTP(h *Hub) http.HandlerFunc {}resp := &api.HubInfoResponse{- OidcIssuer: issuer,- ClientId: aud,- Audience: aud,+ OidcIssuer: issuer,+ ClientId: aud,+ Audience: aud,+ HubAddresses: getMyHubAddrs(h),}respData, err := proto.Marshal(resp)diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex c72b62ad..8374a25b 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -388,6 +388,9 @@ func main() {if err != nil {logger.Fatalf("Enrollment failed: %v", err)}+ if err := store.SaveHubURL(targetHub); err != nil {+ logger.Warnf("Failed to save hub URL: %v", err)+ }fmt.Println("Successfully joined the Sovereign Agent Mesh!")},diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex bd125e04..92f35727 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -251,6 +251,39 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.Pif fatalAuthErr != nil {return nil, fmt.Errorf("fatal auth failure: %w", fatalAuthErr)}++ // Self-healing fallback: try to fetch updated addresses via HTTP if hub_url is known+ hubURL, err := node.Store.LoadHubURL()+ if err == nil && hubURL != "" {+ logger.Infof("[AuthN] All P2P dials failed. Attempting to fetch updated hub addresses via HTTP from %s...", hubURL)+ info, err := node.DiscoverHubInfo(ctx, hubURL)+ if err == nil && len(info.HubAddresses) > 0 {+ logger.Infof("[AuthN] Discovered updated hub addresses: %v", info.HubAddresses)+ var newHubAddrs []multiaddr.Multiaddr+ for _, addrStr := range info.HubAddresses {+ ma, err := multiaddr.NewMultiaddr(addrStr)+ if err == nil {+ newHubAddrs = append(newHubAddrs, ma)+ }+ }+ if len(newHubAddrs) > 0 {+ if pubKeyBytes, _, err := node.Store.LoadHubConfig(); err == nil {+ _ = node.Store.SaveHubConfig(pubKeyBytes, info.HubAddresses)+ }+ for _, addr := range newHubAddrs {+ if err := node.ConnectAndAuthWithHub(ctx, addr); err != nil {+ logger.Warnf("[AuthN] Fallback auth failed with %s: %v", addr, err)+ } else {+ authenticated = true+ logger.Infof("[AuthN] Fallback connection successful!")+ break+ }+ }+ }+ } else {+ logger.Warnf("[AuthN] Failed to fetch updated addresses via HTTP: %v", err)+ }+ }}// Initialize Gossipsub for Hub Eventsdiff --git a/cmd/sam-node/store.go b/cmd/sam-node/store.goindex f7226178..2e57626e 100644--- a/cmd/sam-node/store.go+++ b/cmd/sam-node/store.go@@ -155,6 +155,23 @@ func (s *Store) LoadHubConfig() ([]byte, []string, error) {return pubKey, addrs, err}+func (s *Store) SaveHubURL(url string) error {+ return s.db.Update(func(tx *bbolt.Tx) error {+ b := tx.Bucket([]byte(bucketIdentity))+ return b.Put([]byte("hub_url"), []byte(url))+ })+}++func (s *Store) LoadHubURL() (string, error) {+ var val []byte+ err := s.db.View(func(tx *bbolt.Tx) error {+ b := tx.Bucket([]byte(bucketIdentity))+ val = b.Get([]byte("hub_url"))+ return nil+ })+ return string(val), err+}+func (s *Store) Close() error {return s.db.Close()}diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.goindex 24d52259..ffd5f02b 100644--- a/tests/integration/fallback_test.go+++ b/tests/integration/fallback_test.go@@ -17,6 +17,7 @@ package integration_testimport ("bytes""context"+ "encoding/json""crypto/ed25519""fmt""os"@@ -38,6 +39,7 @@ import ("github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/peer""github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-libp2p/core/host""github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr""google.golang.org/protobuf/proto"@@ -346,3 +348,164 @@ func (s *safeBuffer) String() string {defer s.mu.Unlock()return s.buf.String()}++func TestSelfHealingHTTPFallback(t *testing.T) {+ nodeBin := buildBinary(t, "./cmd/sam-node")++ var mu sync.Mutex+ var currentP2PAddr string++ createNewHost := func() host.Host {+ newH, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ newH.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer func() { _ = s.Close() }()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ defer reader.ReleaseMsg(msg)++ writer := msgio.NewVarintWriter(s)+ resp := &api.AuthResponse{Success: true}+ respBytes, _ := proto.Marshal(resp)+ _ = writer.WriteMsg(respBytes)+ })+ return newH+ }++ h := createNewHost()+ defer func() { _ = h.Close() }()++ mu.Lock()+ currentP2PAddr = h.Addrs()[0].String() + "/p2p/" + h.ID().String()+ mu.Unlock()++ mux := http.NewServeMux()++ // Mock OIDC server for device flow+ mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "issuer": "http://" + r.Host,+ "token_endpoint": "http://" + r.Host + "/token",+ "device_authorization_endpoint": "http://" + r.Host + "/device/code",+ })+ })+ mux.HandleFunc("/device/code", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "device_code": "dev_code_123",+ "user_code": "ABCD-1234",+ "verification_uri": "http://example.com/verify",+ "verification_uri_complete": "http://example.com/verify?code=ABCD-1234",+ "expires_in": 60,+ "interval": 1,+ })+ })+ mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]string{+ "access_token": "test-jwt-token",+ "id_token": "test-jwt-token",+ })+ })++ mux.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {+ mu.Lock()+ addr := currentP2PAddr+ mu.Unlock()+ pub, _, _ := ed25519.GenerateKey(nil)+ resp := &api.EnrollResponse{+ BiscuitToken: []byte("mock-biscuit-token"),+ HubPublicKey: pub,+ HubAddresses: []string{addr},+ }+ data, _ := proto.Marshal(resp)+ w.Header().Set("Content-Type", "application/x-protobuf")+ _, _ = w.Write(data)+ })+ mux.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {+ mu.Lock()+ addr := currentP2PAddr+ mu.Unlock()+ resp := &api.HubInfoResponse{+ OidcIssuer: "http://" + r.Host,+ ClientId: "sam-mesh-audience",+ Audience: "sam-mesh-audience",+ HubAddresses: []string{addr},+ }+ data, _ := proto.Marshal(resp)+ w.Header().Set("Content-Type", "application/x-protobuf")+ _, _ = w.Write(data)+ })++ httpServer := httptest.NewServer(mux)+ defer httpServer.Close()++ tmpHome := t.TempDir()+ env := append(os.Environ(),+ "HOME="+tmpHome,+ "XDG_CONFIG_HOME="+filepath.Join(tmpHome, ".config"),+ )++ // Step 1: Enroll via Join (using mock OIDC)+ joinStdout, joinStderr, err := runCommand(+ t,+ repoRoot(t),+ 5*time.Second,+ env,+ "",+ nodeBin,+ "join",+ httpServer.URL,+ )+ if err != nil {+ t.Fatalf("Join failed: %v\nstdout:\n%s\nstderr:\n%s", err, joinStdout, joinStderr)+ }++ out := joinStdout + joinStderr+ if !strings.Contains(out, "Successfully joined the Sovereign Agent Mesh!") {+ t.Fatalf("Join did not succeed:\n%s", out)+ }++ // Step 2: Simulate Hub changing its P2P port (HTTP URL stays the same)+ _ = h.Close()+ h = createNewHost()+ defer func() { _ = h.Close() }()++ mu.Lock()+ currentP2PAddr = h.Addrs()[0].String() + "/p2p/" + h.ID().String()+ mu.Unlock()++ // Step 3: Start sam-node run+ runCmd := exec.Command(nodeBin, "run", "--listen", "/ip4/127.0.0.1/tcp/0")+ runCmd.Env = env+ var stdout safeBuffer+ runCmd.Stdout = &stdout+ runCmd.Stderr = &stdout++ if err := runCmd.Start(); err != nil {+ t.Fatal(err)+ }+ defer func() { _ = runCmd.Process.Kill() }()++ // Wait for successful fallback+ success := false+ for i := 0; i < 50; i++ {+ out = stdout.String()+ if strings.Contains(out, "Attempting to fetch updated hub addresses via HTTP") &&+ strings.Contains(out, "Fallback connection successful!") {+ success = true+ break+ }+ time.Sleep(100 * time.Millisecond)+ }++ if !success {+ t.Fatalf("Failed to detect self-healing HTTP fallback in output.\nOutput:\n%s", out)+ }+}