Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
add discivery api
Antonio Ojeab36573aedc7d
diff --git a/cmd/sam/proxy.go b/cmd/sam/proxy.goindex c3f1fdd2..dfa66c01 100644--- a/cmd/sam/proxy.go+++ b/cmd/sam/proxy.go@@ -2,22 +2,35 @@ package mainimport ("context"+ "encoding/json""fmt""io""log/slog""math/big""net/http"+ "os"+ "path/filepath"+ "sort""strconv""strings""time"+ "github.com/ipfs/go-cid""github.com/libp2p/go-libp2p/core/peer"+ "github.com/multiformats/go-multihash""github.com/spf13/cobra"+ "go.etcd.io/bbolt"+ internaldb "sam/internal/db"samnet "sam/pkg/net""sam/pkg/protocol")+const (+ proxyCacheRecordVersion = 1+ proxyCacheCardPrefix = "agent-card/"+)+func newProxyCmd(cfg *runConfig) *cobra.Command {cmd := &cobra.Command{Use: "proxy",@@ -62,14 +75,19 @@ func runProxy(parent context.Context, cfg *runConfig) error {}defer func() { _ = node.Stop(context.Background()) }()- observer, err := protocol.NewBoltObserverForFederation(cfg.federation)- if err != nil {- return fmt.Errorf("creating reputation observer: %w", err)- }- defer func() { _ = observer.Close() }()-handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if strings.HasPrefix(r.URL.Path, "/.sam/") {+ handleSAMReserved(w, r, node, cfg)+ return+ }+start := time.Now()+ observer, err := protocol.NewBoltObserverForFederation(cfg.federation)+ if err != nil {+ http.Error(w, "reputation observer unavailable", http.StatusInternalServerError)+ return+ }+ defer func() { _ = observer.Close() }()vouch, vErr := loadLocalVouch()if vErr != nil {@@ -169,6 +187,379 @@ func runProxy(parent context.Context, cfg *runConfig) error {return nil}+func handleSAMReserved(w http.ResponseWriter, r *http.Request, node samnet.Node, cfg *runConfig) {+ ctx, cancel := context.WithTimeout(r.Context(), cfg.proxyTimeout)+ defer cancel()++ switch r.URL.Path {+ case "/.sam/inventory":+ if r.Method != http.MethodGet {+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)+ return+ }+ cards, err := aggregateInventory(ctx, node, cfg.federation)+ if err != nil {+ http.Error(w, fmt.Sprintf("inventory lookup failed: %v", err), http.StatusBadGateway)+ return+ }+ writeJSON(w, cards)+ case "/.sam/mcp/inventory":+ if r.Method != http.MethodGet {+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)+ return+ }+ cards, err := aggregateInventory(ctx, node, cfg.federation)+ if err != nil {+ http.Error(w, fmt.Sprintf("inventory lookup failed: %v", err), http.StatusBadGateway)+ return+ }+ writeJSON(w, buildGlobalMCPCatalog(cards))+ case "/.sam/search":+ if r.Method != http.MethodGet {+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)+ return+ }+ skill := strings.TrimSpace(r.URL.Query().Get("skill"))+ if skill == "" {+ http.Error(w, "missing skill query parameter", http.StatusBadRequest)+ return+ }+ cards, err := searchInventoryBySkill(ctx, node, cfg.federation, skill)+ if err != nil {+ http.Error(w, fmt.Sprintf("search failed: %v", err), http.StatusBadGateway)+ return+ }+ writeJSON(w, cards)+ default:+ http.NotFound(w, r)+ }+}++func aggregateInventory(ctx context.Context, node samnet.Node, federation string) ([]*protocol.AgentCard, error) {+ cached, err := loadCachedCards(federation)+ if err != nil {+ return nil, err+ }++ byPeer := make(map[string]*protocol.AgentCard, len(cached))+ capabilities := map[string]struct{}{}+ for _, card := range cached {+ if card == nil || strings.TrimSpace(card.PeerID) == "" {+ continue+ }+ byPeer[card.PeerID] = card+ for _, capName := range card.CapabilityNames() {+ capabilities[capName] = struct{}{}+ }+ }++ for capability := range capabilities {+ liveCards, err := discoverCardsByCapability(ctx, node, capability)+ if err != nil {+ continue+ }+ for _, card := range liveCards {+ if card == nil || strings.TrimSpace(card.PeerID) == "" {+ continue+ }+ byPeer[card.PeerID] = card+ _ = cacheCard(federation, card)+ }+ }++ out := make([]*protocol.AgentCard, 0, len(byPeer))+ for _, card := range byPeer {+ out = append(out, card)+ }+ sort.Slice(out, func(i, j int) bool { return out[i].PeerID < out[j].PeerID })+ return out, nil+}++func searchInventoryBySkill(ctx context.Context, node samnet.Node, federation, skill string) ([]*protocol.AgentCard, error) {+ byPeer := map[string]*protocol.AgentCard{}++ cached, _ := loadCachedCards(federation)+ for _, card := range cached {+ if cardHasCapability(card, skill) {+ byPeer[card.PeerID] = card+ }+ }++ liveCards, err := discoverCardsByCapability(ctx, node, skill)+ if err != nil {+ if len(byPeer) == 0 {+ return nil, err+ }+ } else {+ for _, card := range liveCards {+ if card == nil || strings.TrimSpace(card.PeerID) == "" {+ continue+ }+ byPeer[card.PeerID] = card+ _ = cacheCard(federation, card)+ }+ }++ out := make([]*protocol.AgentCard, 0, len(byPeer))+ for _, card := range byPeer {+ out = append(out, card)+ }+ sort.Slice(out, func(i, j int) bool { return out[i].PeerID < out[j].PeerID })+ return out, nil+}++func discoverCardsByCapability(ctx context.Context, node samnet.Node, capability string) ([]*protocol.AgentCard, error) {+ ns := protocol.AgentCapabilityNamespace(capability)+ peerInfos, err := findProvidersForNamespace(ctx, node, ns)+ if err != nil {+ return nil, err+ }++ seen := map[string]struct{}{}+ cards := make([]*protocol.AgentCard, 0, len(peerInfos))+ for _, pi := range peerInfos {+ if pi.ID == "" {+ continue+ }+ pid := pi.ID.String()+ if _, ok := seen[pid]; ok {+ continue+ }+ seen[pid] = struct{}{}++ if len(pi.Addrs) > 0 {+ _ = node.Connect(ctx, pi)+ }+ card, err := fetchCardFromDHT(ctx, node, pid)+ if err != nil {+ continue+ }+ if !cardHasCapability(card, capability) {+ continue+ }+ cards = append(cards, card)+ }+ return cards, nil+}++func findProvidersForNamespace(ctx context.Context, node samnet.Node, namespace string) ([]peer.AddrInfo, error) {+ d := node.DHT()+ if d == nil {+ return nil, fmt.Errorf("dht is not available")+ }+ topicCID, err := namespaceToCID(namespace)+ if err != nil {+ return nil, err+ }++ results := make([]peer.AddrInfo, 0)+ seen := map[string]struct{}{}+ for info := range d.FindProvidersAsync(ctx, topicCID, 64) {+ if info.ID == "" || info.ID == node.PeerID() {+ continue+ }+ if _, ok := seen[info.ID.String()]; ok {+ continue+ }+ seen[info.ID.String()] = struct{}{}+ results = append(results, info)+ }+ return results, nil+}++func namespaceToCID(namespace string) (cid.Cid, error) {+ namespace = strings.TrimSpace(namespace)+ if namespace == "" {+ return cid.Undef, fmt.Errorf("namespace is required")+ }+ h, err := multihash.Sum([]byte(namespace), multihash.SHA2_256, -1)+ if err != nil {+ return cid.Undef, fmt.Errorf("hashing namespace: %w", err)+ }+ return cid.NewCidV1(cid.Raw, h), nil+}++func fetchCardFromDHT(ctx context.Context, node samnet.Node, peerID string) (*protocol.AgentCard, error) {+ raw, err := node.GetValue(ctx, protocol.DHTPeerCardKey(peerID))+ if err != nil {+ return nil, err+ }+ var card protocol.AgentCard+ if err := json.Unmarshal(raw, &card); err != nil {+ return nil, fmt.Errorf("decoding agent card: %w", err)+ }+ if err := protocol.VerifyAgentCard(&card); err != nil {+ return nil, fmt.Errorf("validating agent card: %w", err)+ }+ return &card, nil+}++func cardHasCapability(card *protocol.AgentCard, capability string) bool {+ if card == nil {+ return false+ }+ want := strings.ToLower(strings.TrimSpace(capability))+ if want == "" {+ return false+ }+ for _, c := range card.CapabilityNames() {+ if strings.EqualFold(c, want) {+ return true+ }+ }+ return false+}++func cacheCard(federation string, card *protocol.AgentCard) error {+ if card == nil || strings.TrimSpace(card.PeerID) == "" {+ return nil+ }+ path, err := federationDBPath(federation)+ if err != nil {+ return err+ }+ db, err := bbolt.Open(path, 0o600, &bbolt.Options{Timeout: 2 * time.Second})+ if err != nil {+ return err+ }+ defer func() { _ = db.Close() }()++ codec := internaldb.JSONCodec{}+ payload, err := codec.Marshal(proxyCacheRecordVersion, card)+ if err != nil {+ return err+ }+ key := proxyCacheCardPrefix + card.PeerID+ return db.Update(func(tx *bbolt.Tx) error {+ bucket := tx.Bucket([]byte(internaldb.BucketCache))+ if bucket == nil {+ return fmt.Errorf("missing cache bucket")+ }+ return bucket.Put([]byte(key), payload)+ })+}++func loadCachedCards(federation string) ([]*protocol.AgentCard, error) {+ path, err := federationDBPath(federation)+ if err != nil {+ return nil, err+ }+ if _, err := os.Stat(path); err != nil {+ if os.IsNotExist(err) {+ return nil, nil+ }+ return nil, err+ }+ db, err := bbolt.Open(path, 0o600, &bbolt.Options{Timeout: 2 * time.Second, ReadOnly: true})+ if err != nil {+ return nil, err+ }+ defer func() { _ = db.Close() }()++ codec := internaldb.JSONCodec{}+ seen := map[string]struct{}{}+ out := make([]*protocol.AgentCard, 0)+ err = db.View(func(tx *bbolt.Tx) error {+ bucket := tx.Bucket([]byte(internaldb.BucketCache))+ if bucket == nil {+ return nil+ }+ return bucket.ForEach(func(key, value []byte) error {+ if !strings.HasPrefix(string(key), proxyCacheCardPrefix) {+ return nil+ }+ var card protocol.AgentCard+ if err := codec.Unmarshal(value, proxyCacheRecordVersion, &card, nil); err != nil {+ return nil+ }+ if err := protocol.VerifyAgentCard(&card); err != nil {+ return nil+ }+ if _, ok := seen[card.PeerID]; ok {+ return nil+ }+ seen[card.PeerID] = struct{}{}+ out = append(out, &card)+ return nil+ })+ })+ if err != nil {+ return nil, err+ }+ return out, nil+}++func federationDBPath(federation string) (string, error) {+ baseDir, err := internaldb.FederationsDir()+ if err != nil {+ return "", err+ }+ name := strings.TrimSpace(federation)+ if name == "" {+ name = "default"+ }+ if err := os.MkdirAll(baseDir, 0o700); err != nil {+ return "", err+ }+ return filepath.Join(baseDir, name+".db"), nil+}++type meshToolEntry struct {+ URI string `json:"uri"`+ PeerID string `json:"peer_id"`+ Name string `json:"name"`+ Kind string `json:"kind,omitempty"`+ Capability string `json:"capability,omitempty"`+ Description string `json:"description,omitempty"`+}++func buildGlobalMCPCatalog(cards []*protocol.AgentCard) []meshToolEntry {+ out := make([]meshToolEntry, 0)+ for _, card := range cards {+ if card == nil || strings.TrimSpace(card.PeerID) == "" {+ continue+ }+ for _, res := range card.Resources {+ name := strings.TrimSpace(res.Name)+ if name == "" {+ continue+ }+ out = append(out, meshToolEntry{+ URI: fmt.Sprintf("mesh://%s/%s", card.PeerID, name),+ PeerID: card.PeerID,+ Name: name,+ Kind: strings.TrimSpace(res.Kind),+ Description: strings.TrimSpace(res.Description),+ })+ }+ if len(card.Resources) > 0 {+ continue+ }+ for _, capability := range card.CapabilityNames() {+ out = append(out, meshToolEntry{+ URI: fmt.Sprintf("mesh://%s/%s", card.PeerID, capability),+ PeerID: card.PeerID,+ Name: capability,+ Capability: capability,+ })+ }+ }+ sort.Slice(out, func(i, j int) bool {+ if out[i].PeerID == out[j].PeerID {+ return out[i].Name < out[j].Name+ }+ return out[i].PeerID < out[j].PeerID+ })+ return out+}++func writeJSON(w http.ResponseWriter, v any) {+ w.Header().Set("Content-Type", "application/json")+ if err := json.NewEncoder(w).Encode(v); err != nil {+ http.Error(w, fmt.Sprintf("encoding response: %v", err), http.StatusInternalServerError)+ }+}+func resolveProxyTarget(ctx context.Context, node samnet.Node, targetArg string) (peer.AddrInfo, string, error) {targetArg = strings.TrimSpace(targetArg)if targetArg == "" {diff --git a/cmd/sam/proxy_reserved_test.go b/cmd/sam/proxy_reserved_test.gonew file mode 100644index 00000000..45eff59b--- /dev/null+++ b/cmd/sam/proxy_reserved_test.go@@ -0,0 +1,161 @@+package main+package main++import (+ "context"+ "encoding/json"+ "fmt"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "testing"+ "time"++ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/multiformats/go-multiaddr"++ samnet "sam/pkg/net"+ "sam/pkg/protocol"+)++func TestSAMReservedSearchReturnsPublishedWriterCard(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()++ tmpHome := t.TempDir()+ t.Setenv("HOME", tmpHome)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpHome, ".config"))++ nodeB, err := newProxyReservedNode(0, nil)+ if err != nil {+ t.Fatalf("creating proxy/search node: %v", err)+ }+ if err := nodeB.Start(ctx); err != nil {+ t.Fatalf("starting proxy/search node: %v", err)+ }+ defer func() { _ = nodeB.Stop(context.Background()) }()++ bootstrap := []multiaddr.Multiaddr{nodeB.Addrs()[0].Encapsulate(multiaddr.StringCast("/p2p/" + nodeB.PeerID().String()))}+ nodeA, err := newProxyReservedNode(0, bootstrap)+ if err != nil {+ t.Fatalf("creating publisher node: %v", err)+ }+ if err := nodeA.Start(ctx); err != nil {+ t.Fatalf("starting publisher node: %v", err)+ }+ defer func() { _ = nodeA.Stop(context.Background()) }()++ if err := connectProxyReservedWithRetry(ctx, nodeA, peer.AddrInfo{ID: nodeB.PeerID(), Addrs: nodeB.Addrs()}); err != nil {+ t.Fatalf("connecting publisher to search node: %v", err)+ }+ if err := connectProxyReservedWithRetry(ctx, nodeB, peer.AddrInfo{ID: nodeA.PeerID(), Addrs: nodeA.Addrs()}); err != nil {+ t.Fatalf("connecting search node to publisher: %v", err)+ }++ priv := nodeA.Host().Peerstore().PrivKey(nodeA.PeerID())+ if priv == nil {+ t.Fatalf("publisher private key missing")+ }+ card, err := protocol.NewAgentCard(nodeA.PeerID(), []string{"writer"}, []protocol.MCPResource{{Name: "writer", Kind: "tool", Endpoint: "mcp://writer"}}, priv)+ if err != nil {+ t.Fatalf("creating writer card: %v", err)+ }+ if err := attachNodeVouch(card, nodeA.PeerID().String(), priv); err != nil {+ t.Fatalf("attaching card vouch: %v", err)+ }+ pub, err := protocol.NewPublisher(nodeA)+ if err != nil {+ t.Fatalf("creating publisher: %v", err)+ }+ if err := publishProxyReservedWithRetry(ctx, pub, card); err != nil {+ t.Fatalf("publishing writer card: %v", err)+ }++ cfg := &runConfig{proxyTimeout: 3 * time.Second, federation: "default"}++ deadline := time.Now().Add(12 * time.Second)+ for {+ req := httptest.NewRequest(http.MethodGet, "/.sam/search?skill=writer", nil)+ rr := httptest.NewRecorder()+ handleSAMReserved(rr, req, nodeB, cfg)++ if rr.Code == http.StatusOK {+ var cards []*protocol.AgentCard+ if err := json.Unmarshal(rr.Body.Bytes(), &cards); err != nil {+ t.Fatalf("decoding search response: %v", err)+ }+ for _, got := range cards {+ if got != nil && got.PeerID == nodeA.PeerID().String() {+ if got.Vouch == nil || got.Vouch.Signature == "" {+ t.Fatalf("expected signed vouch in search response for %s", got.PeerID)+ }+ return+ }+ }+ }++ if time.Now().After(deadline) {+ t.Fatalf("writer card not discovered in /.sam/search response before timeout")+ }+ time.Sleep(250 * time.Millisecond)+ }+}++func newProxyReservedNode(port int, bootstrap []multiaddr.Multiaddr) (samnet.Node, error) {+ listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/udp/%d/quic-v1", port))+ if err != nil {+ return nil, fmt.Errorf("building listen address: %w", err)+ }+ key, err := samnet.GenerateKey()+ if err != nil {+ return nil, fmt.Errorf("generating node key: %w", err)+ }+ return samnet.New(+ samnet.WithPrivateKey(key),+ samnet.WithListenAddrs(listen),+ samnet.WithBootstrapPeers(bootstrap...),+ samnet.WithDHTMode(samnet.DHTModeServer),+ )+}++func connectProxyReservedWithRetry(ctx context.Context, node samnet.Node, pi peer.AddrInfo) error {+ ticker := time.NewTicker(250 * time.Millisecond)+ defer ticker.Stop()+ var last error+ for {+ if err := node.Connect(ctx, pi); err == nil {+ return nil+ } else {+ last = err+ }+ select {+ case <-ctx.Done():+ if last != nil {+ return last+ }+ return ctx.Err()+ case <-ticker.C:+ }+ }+}++func publishProxyReservedWithRetry(ctx context.Context, pub *protocol.Publisher, card *protocol.AgentCard) error {+ ticker := time.NewTicker(250 * time.Millisecond)+ defer ticker.Stop()+ var last error+ for {+ if err := pub.Publish(ctx, card); err == nil {+ return nil+ } else {+ last = err+ }+ select {+ case <-ctx.Done():+ if last != nil {+ return last+ }+ return ctx.Err()+ case <-ticker.C:+ }+ }+}diff --git a/cmd/sam/publish.go b/cmd/sam/publish.goindex e00e3875..b696a1dc 100644--- a/cmd/sam/publish.go+++ b/cmd/sam/publish.go@@ -2,6 +2,7 @@ package mainimport ("context"+ "crypto/ed25519""encoding/json""fmt""net/http"@@ -13,6 +14,7 @@ import ("github.com/spf13/cobra""sam/pkg/economy"+ "sam/pkg/identity"samnet "sam/pkg/net""sam/pkg/protocol")@@ -106,6 +108,9 @@ func runPublish(parent context.Context, cfg *runConfig) error {if err != nil {return fmt.Errorf("building agent card: %w", err)}+ if err := attachNodeVouch(card, node.PeerID().String(), priv); err != nil {+ return err+ }// For dry-run=server mode, skip DHT but still build networkif cfg.dryRun == "server" {@@ -134,6 +139,45 @@ func runPublish(parent context.Context, cfg *runConfig) error {return waitForShutdown(parent, cfg.runFor)}+func attachNodeVouch(card *protocol.AgentCard, peerID string, priv any) error {+ if card == nil {+ return fmt.Errorf("agent card is nil")+ }+ if vouch, err := loadLocalVouch(); err == nil && vouch != nil {+ card.AttachVouch(vouch)+ return nil+ }++ rawKey, err := nodeEd25519PrivateKey(priv)+ if err != nil {+ return fmt.Errorf("extracting ed25519 key for self-vouch: %w", err)+ }+ vouch, err := identity.SelfSignVouch(peerID, rawKey)+ if err != nil {+ return fmt.Errorf("building self-signed vouch: %w", err)+ }+ card.AttachVouch(vouch)+ return nil+}++func nodeEd25519PrivateKey(priv any) (ed25519.PrivateKey, error) {+ type rawKeyProvider interface {+ Raw() ([]byte, error)+ }+ rawProvider, ok := priv.(rawKeyProvider)+ if !ok {+ return nil, fmt.Errorf("unsupported private key type")+ }+ raw, err := rawProvider.Raw()+ if err != nil {+ return nil, err+ }+ if len(raw) != ed25519.PrivateKeySize {+ return nil, fmt.Errorf("unexpected private key size %d", len(raw))+ }+ return ed25519.PrivateKey(raw), nil+}+func publishLoop(parent context.Context, pub *protocol.Publisher, card *protocol.AgentCard, every time.Duration) error {if every <= 0 {every = 2 * time.Minutediff --git a/cmd/sam/publish_card.go b/cmd/sam/publish_card.goindex 2369e7f6..94a8d1a9 100644--- a/cmd/sam/publish_card.go+++ b/cmd/sam/publish_card.go@@ -55,6 +55,9 @@ func runPublishCard(parent context.Context, cfg *runConfig) error {if err != nil {return fmt.Errorf("building agent card: %w", err)}+ if err := attachNodeVouch(card, node.PeerID().String(), priv); err != nil {+ return err+ }pub, err := protocol.NewPublisher(node)if err != nil {diff --git a/cmd/sam/publish_mcp.go b/cmd/sam/publish_mcp.goindex d3a1713e..a036a032 100644--- a/cmd/sam/publish_mcp.go+++ b/cmd/sam/publish_mcp.go@@ -63,6 +63,9 @@ func runPublishMCP(parent context.Context, cfg *runConfig) error {if err != nil {return fmt.Errorf("building agent card: %w", err)}+ if err := attachNodeVouch(card, node.PeerID().String(), priv); err != nil {+ return err+ }connector := &httpMCPConnector{client: &http.Client{Timeout: 30 * time.Second},diff --git a/hack/lint.sh b/hack/lint.shindex a88eb1b4..310cf608 100755--- a/hack/lint.sh+++ b/hack/lint.sh@@ -7,4 +7,4 @@ set -o pipefailREPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..cd $REPO_ROOT-docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v2.11.4 golangci-lint run -v+docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v2.11.4 golangci-lint run -v --skip-dirs sam_test_tmpdiff --git a/tests/e2e/sam.bats b/tests/e2e/sam.batsindex 7c3a5ef5..04417376 100644--- a/tests/e2e/sam.bats+++ b/tests/e2e/sam.bats@@ -124,3 +124,57 @@ teardown() {run cat "$TEST_TMPDIR/proxy_body.txt"[[ "$output" == *"unauthorized"* ]]}++@test "sam proxy /.sam/search discovers writer skill with vouch" {+ node_a_log="$TEST_TMPDIR/node-a.log"+ node_b_log="$TEST_TMPDIR/node-b.log"+ proxy_port=18081+ node_a_listen="/ip4/127.0.0.1/udp/4313/quic-v1"+ node_b_listen="/ip4/127.0.0.1/udp/4314/quic-v1"++ "$SAM_BINARY" proxy --port "$proxy_port" --listen "$node_b_listen" --dht-mode server --run-for 20s >"$node_b_log" 2>&1 &+ node_b_pid=$!++ node_b_peer=""+ for _ in {1..100}; do+ node_b_peer="$(grep -o 'peer_id=[^ ]*' "$node_b_log" | head -n1 | cut -d= -f2)"+ if [[ -n "$node_b_peer" ]]; then+ break+ fi+ sleep 0.1+ done+ [[ -n "$node_b_peer" ]]++ node_a_peer=""+ "$SAM_BINARY" publish --skill writer --mcp-port 19099 --listen "$node_a_listen" --bootstrap "/ip4/127.0.0.1/udp/4314/quic-v1/p2p/${node_b_peer}" --dht-mode server --run-for 20s >"$node_a_log" 2>&1 &+ node_a_pid=$!++ for _ in {1..120}; do+ node_a_peer="$(grep -o 'peer_id=[^ ]*' "$node_a_log" | head -n1 | cut -d= -f2)"+ if [[ -n "$node_a_peer" ]]; then+ break+ fi+ sleep 0.1+ done+ [[ -n "$node_a_peer" ]]++ search_status="000"+ for _ in {1..120}; do+ search_status="$(curl -s -o "$TEST_TMPDIR/search_body.json" -w "%{http_code}" "http://127.0.0.1:${proxy_port}/.sam/search?skill=writer" || true)"+ if [[ "$search_status" == "200" ]] && grep -q "$node_a_peer" "$TEST_TMPDIR/search_body.json"; then+ break+ fi+ sleep 0.2+ done++ kill "$node_b_pid" >/dev/null 2>&1 || true+ wait "$node_b_pid" >/dev/null 2>&1 || true+ kill "$node_a_pid" >/dev/null 2>&1 || true+ wait "$node_a_pid" >/dev/null 2>&1 || true++ [[ "$search_status" == "200" ]]+ run cat "$TEST_TMPDIR/search_body.json"+ [[ "$output" == *"$node_a_peer"* ]]+ [[ "$output" == *'"vouch"'* ]]+ [[ "$output" == *'"signature"'* ]]+}