Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: ask the directory again after a lookup that found nothing
Antonio Ojea5da6337d13a4
diff --git a/internal/node/directory_client.go b/internal/node/directory_client.goindex 1a9aa9b5..dd709d43 100644--- a/internal/node/directory_client.go+++ b/internal/node/directory_client.go@@ -293,11 +293,15 @@ func addrInfoOf(rec *api.PeerRecord) (peer.AddrInfo, error) {return info, nil}-// rememberAddrs keeps what a lookup learned for a dial that follows.+// rememberAddrs keeps what a lookup learned for a dial that follows. An+// answer from the directory lifts the unresolved mark a failed lookup left.func rememberAddrs(ps peerstore.Peerstore, infos ...peer.AddrInfo) {for _, info := range infos {if len(info.Addrs) > 0 {ps.AddAddrs(info.ID, info.Addrs, peerstore.TempAddrTTL)+ if err := ps.Put(info.ID, PeerstoreKeyUnresolved, false); err != nil {+ logger.Errorf("[Directory] Failed to clear peerstore unresolved key for %s: %v", info.ID, err)+ }}}}diff --git a/internal/node/mcp.go b/internal/node/mcp.goindex ed7e606f..dca899bf 100644--- a/internal/node/mcp.go+++ b/internal/node/mcp.go@@ -475,11 +475,23 @@ func (n *AgentMeshNode) preparePeerAddrs(ctx context.Context, targetPeer peer.IDchanged := falseaddrs := n.Host.Peerstore().Addrs(targetPeer)- if len(addrs) == 0 {- logger.Debugf("[Discovery] No addresses in peerstore for %s, asking the directory", targetPeer)+ unresolved := false+ if v, err := n.Host.Peerstore().Get(targetPeer, PeerstoreKeyUnresolved); err == nil {+ unresolved, _ = v.(bool)+ }+ lookedUp := false+ if len(addrs) == 0 || unresolved {+ if unresolved {+ logger.Debugf("[Discovery] Peerstore holds only this node's fallback circuits for %s, asking the directory again", targetPeer)+ } else {+ logger.Debugf("[Discovery] No addresses in peerstore for %s, asking the directory", targetPeer)+ }+ lookedUp = truefindCtx, cancel := context.WithTimeout(ctx, dhtLookupTimeout)addrInfo, err := n.Directory.FindPeer(findCtx, targetPeer)cancel()+ // Nothing from the directory leaves addrs empty, so the fallback+ // below is rebuilt from the routers this node holds now.addrs = addrInfo.Addrsswitch {case len(addrs) > 0:@@ -578,6 +590,15 @@ func (n *AgentMeshNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID}}+ // The directory was asked and said nothing: whatever is dialed now is+ // this node's guess, so the next lookup asks again. An answer, with+ // addresses, lifts the mark.+ if lookedUp {+ if err := n.Host.Peerstore().Put(targetPeer, PeerstoreKeyUnresolved, len(addrs) == 0); err != nil {+ logger.Errorf("[Discovery] Failed to put peerstore unresolved key for %s: %v", targetPeer, err)+ }+ }+if kept := n.selectTransit(ctx, validAddrs); len(kept) != len(validAddrs) {validAddrs = keptchanged = truediff --git a/internal/node/mcp_test.go b/internal/node/mcp_test.goindex ca2f04f8..d88a9cd1 100644--- a/internal/node/mcp_test.go+++ b/internal/node/mcp_test.go@@ -16,6 +16,7 @@ package nodeimport ("context"+ "errors""fmt""net/http""net/http/httptest"@@ -220,6 +221,102 @@ func TestPreparePeerAddrsFallsBackToAuthenticatedRouters(t *testing.T) {}}+// scriptedDirectory answers FindPeer from a queue of outcomes and counts the+// calls; the other methods are not reached by preparePeerAddrs.+type scriptedDirectory struct {+ findPeer []func(peer.ID) (peer.AddrInfo, error)+ calls int+}++func (d *scriptedDirectory) Provide(context.Context, []byte, string) error { return nil }+func (d *scriptedDirectory) Withdraw(context.Context, []byte) error { return nil }+func (d *scriptedDirectory) FindProviders(context.Context, []byte, int) ([]peer.AddrInfo, error) {+ return nil, nil+}+func (d *scriptedDirectory) FindPeer(_ context.Context, p peer.ID) (peer.AddrInfo, error) {+ d.calls+++ if len(d.findPeer) == 0 {+ return peer.AddrInfo{ID: p}, errors.New("no scripted answer")+ }+ next := d.findPeer[0]+ d.findPeer = d.findPeer[1:]+ return next(p)+}++// A directory lookup that fails leaves the peer with circuits through this+// node's routers for the dial at hand, and nothing more: the next lookup asks+// the directory again instead of dialing the guess for TempAddrTTL. Once the+// directory answers, the peer's own addresses are dialed (our circuits stay+// as a second path) and the directory is not asked again while they last.+func TestPreparePeerAddrsAsksAgainAfterFailedLookup(t *testing.T) {+ ctx := context.Background()+ mn := mocknet.New()+ localHost, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ admitted, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ target, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ _ = mn.LinkAll()+ if _, err := mn.ConnectPeers(localHost.ID(), admitted.ID()); err != nil {+ t.Fatal(err)+ }+ // A node holds its router's addresses; mocknet does not fill them in.+ localHost.Peerstore().AddAddrs(admitted.ID(), admitted.Addrs(), peerstore.PermanentAddrTTL)+ ownCircuit := fmt.Sprintf("/p2p/%s/p2p-circuit", admitted.ID())+ announced := multiaddr.StringCast("/ip4/203.0.113.7/tcp/4501")++ dir := &scriptedDirectory{findPeer: []func(peer.ID) (peer.AddrInfo, error){+ func(p peer.ID) (peer.AddrInfo, error) {+ return peer.AddrInfo{ID: p}, errors.New("i/o deadline reached")+ },+ func(p peer.ID) (peer.AddrInfo, error) {+ return peer.AddrInfo{ID: p, Addrs: []multiaddr.Multiaddr{announced}}, nil+ },+ }}+ node := &AgentMeshNode{+ Host: localHost,+ Directory: dir,+ BiscuitTimeout: 500 * time.Millisecond,+ authenticatedRouters: map[peer.ID]bool{admitted.ID(): true},+ }+ addrsOf := func() []string {+ var got []string+ for _, a := range localHost.Peerstore().Addrs(target.ID()) {+ got = append(got, a.String())+ }+ sort.Strings(got)+ return got+ }++ node.preparePeerAddrs(ctx, target.ID())+ if dir.calls != 1 {+ t.Fatalf("directory calls after a failed lookup = %d, want 1", dir.calls)+ }+ if got := addrsOf(); len(got) != 1 || got[0] != ownCircuit {+ t.Fatalf("addresses after a failed lookup = %v, want only the fallback circuit %s", got, ownCircuit)+ }++ node.preparePeerAddrs(ctx, target.ID())+ if dir.calls != 2 {+ t.Fatalf("directory calls after the fallback was cached = %d, want 2: the guess must not stand in for a record", dir.calls)+ }+ if got := addrsOf(); !slices.Contains(got, announced.String()) {+ t.Fatalf("addresses after the directory answered = %v, want %s among them", got, announced)+ }++ node.preparePeerAddrs(ctx, target.ID())+ if dir.calls != 2 {+ t.Fatalf("directory calls with a record in the peerstore = %d, want 2", dir.calls)+ }+}+func TestSplitToolName(t *testing.T) {tests := []struct {name stringdiff --git a/internal/node/node.go b/internal/node/node.goindex 463c6954..d65839c8 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -73,6 +73,15 @@ import (// subsequent discovery or tool calls when dialing unroutable private networks.const PeerstoreKeyPrivateIPFailed = "private_ip_failed"+// PeerstoreKeyUnresolved marks a peer whose addresses in the Peerstore are+// this node's own fallback, circuits through the routers that admitted this+// node, written because the directory knew no address for the peer or did+// not answer. Those circuits are kept for TempAddrTTL so that the dial has+// them, but they are a guess, not a record: while the mark is set the next+// lookup asks the directory again instead of trusting them, so a lookup that+// timed out once does not stand for two minutes.+const PeerstoreKeyUnresolved = "directory_unresolved"+const (// Cache sizesRateLimiterSize = 1000