Skip to content

Agent Mesh

fix race

Antonio Ojeafbc17e549d9b
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index fe1f252d..de7ff2a5 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -112,23 +112,13 @@ type relayACL struct {
}
func (a *relayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
- for i := 0; i < 20; i++ {
- if _, ok := a.hub.authenticatedPeers.Load(p); ok {
- return true
- }
- time.Sleep(250 * time.Millisecond)
- }
- return false
+ _, ok := a.hub.authenticatedPeers.Load(p)
+ return ok
}
func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
- for i := 0; i < 20; i++ {
- if _, ok := a.hub.authenticatedPeers.Load(src); ok {
- return true
- }
- time.Sleep(250 * time.Millisecond)
- }
- return false
+ _, ok := a.hub.authenticatedPeers.Load(src)
+ return ok
}
// Hub handles identity bridging and network discovery
diff --git a/cmd/sam-node/mcp_handlers.go b/cmd/sam-node/mcp_handlers.go
index ca4af7da..89affc63 100644
--- a/cmd/sam-node/mcp_handlers.go
+++ b/cmd/sam-node/mcp_handlers.go
@@ -191,7 +191,7 @@ func (n *SamNode) handleGetMeshInfo(ctx context.Context, req *mcp.CallToolReques
type CallRemoteToolParams struct {
PeerID string `json:"peer_id" jsonschema:"The Peer ID of the target agent"`
ToolName string `json:"tool_name" jsonschema:"The name of the tool to call"`
- Arguments string `json:"arguments" jsonschema:"JSON encoded arguments for the tool"`
+ Arguments any `json:"arguments" jsonschema:"Arguments for the tool"`
}
// handleCallRemoteTool implements the call_remote_tool tool.
@@ -202,9 +202,16 @@ func (n *SamNode) handleCallRemoteTool(ctx context.Context, req *mcp.CallToolReq
return nil, nil, err
}
var args map[string]any
- if params.Arguments != "" {
- if err := json.Unmarshal([]byte(params.Arguments), &args); err != nil {
- return nil, nil, err
+ if params.Arguments != nil {
+ if m, ok := params.Arguments.(map[string]any); ok {
+ args = m
+ } else {
+ // fallback attempt if it's somehow passed as a string
+ if s, ok := params.Arguments.(string); ok && s != "" {
+ if err := json.Unmarshal([]byte(s), &args); err != nil {
+ return nil, nil, err
+ }
+ }
}
}
res, err := n.CallMCPTool(ctx, targetPeer, params.ToolName, args)
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 6c4fc47b..b6227e52 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -47,6 +47,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/discovery/util"
+ "github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
@@ -81,23 +82,13 @@ type nodeRelayACL struct {
}
func (a *nodeRelayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
- for i := 0; i < 20; i++ {
- if _, ok := a.node.authPeers.Load(p); ok {
- return true
- }
- time.Sleep(250 * time.Millisecond)
- }
- return false
+ _, ok := a.node.authPeers.Load(p)
+ return ok
}
func (a *nodeRelayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
- for i := 0; i < 20; i++ {
- if _, ok := a.node.authPeers.Load(src); ok {
- return true
- }
- time.Sleep(250 * time.Millisecond)
- }
- return false
+ _, ok := a.node.authPeers.Load(src)
+ return ok
}
type SamNode struct {
@@ -199,7 +190,11 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
// If we have a Hub, configure it as our static fallback relay for NAT hole-punching
if len(staticRelays) > 0 {
- opts = append(opts, libp2p.EnableAutoRelayWithStaticRelays(staticRelays))
+ opts = append(opts, libp2p.EnableAutoRelayWithStaticRelays(
+ staticRelays,
+ autorelay.WithBootDelay(2*time.Second),
+ autorelay.WithBackoff(3*time.Second),
+ ))
}
// If the user explicitly opts in, allow this node to serve as a relay for others