Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix keep alive logic
Antonio Ojead8dd25f8f308
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 561313ae..86366940 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -379,9 +379,47 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.Preturn nil, fmt.Errorf("failed to start ingress server: %w", err)}+ // Start connection monitor+ node.startConnectionMonitor(ctx, 2*time.Minute, 1*time.Minute, 3)+return node, nil}+func (n *SamNode) startConnectionMonitor(ctx context.Context, bootstrapDuration, checkInterval time.Duration, maxFailures int) {+ go func() {+ // Wait for initial bootstrap to complete+ select {+ case <-ctx.Done():+ return+ case <-time.After(bootstrapDuration):+ }++ ticker := time.NewTicker(checkInterval)+ defer ticker.Stop()++ consecutiveFailures := 0+ for {+ select {+ case <-ctx.Done():+ return+ case <-ticker.C:+ if len(n.Host.Network().Peers()) == 0 {+ consecutiveFailures+++ logger.Warnf("Not connected to the mesh (0 peers). Consecutive failures: %d/%d", consecutiveFailures, maxFailures)+ if consecutiveFailures >= maxFailures {+ logger.Fatalf("Not connected to the mesh (0 peers) for %d consecutive checks. Exiting to avoid network partition.", maxFailures)+ }+ } else {+ if consecutiveFailures > 0 {+ logger.Infof("Reconnected to the mesh. Resetting failure count.")+ }+ consecutiveFailures = 0+ }+ }+ }+ }()+}+func (n *SamNode) RegisterStaticServices(ctx context.Context, services []api.ServiceConfig) error {// Wait for DHT to be ready (size > 0)// This avoids failure if we try to register immediately after enrollmentdiff --git a/cmd/sam-node/node_test.go b/cmd/sam-node/node_test.goindex cd715063..e7a3a16d 100644--- a/cmd/sam-node/node_test.go+++ b/cmd/sam-node/node_test.go@@ -19,14 +19,12 @@ import ("crypto/ed25519""os""os/exec"- "strings""testing""time""github.com/google/sam/api"lru "github.com/hashicorp/golang-lru/v2""github.com/libp2p/go-libp2p/core/crypto"- "github.com/multiformats/go-multiaddr")func TestHandleBannedEvent(t *testing.T) {@@ -69,20 +67,6 @@ func TestHandleKeyRotationEvent(t *testing.T) {}}-func TestNewSamNode_FailsAuth(t *testing.T) {- priv, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)- hubAddrs := []multiaddr.Multiaddr{multiaddr.StringCast("/ip4/127.0.0.1/tcp/9999")}- store, _ := NewStore(t.TempDir()) // We need a valid store-- _, err := NewSamNode(context.Background(), priv, nil, hubAddrs, store, "test", "10s", []string{"/ip4/127.0.0.1/tcp/0"}, false, nil, 0, false)- if err == nil {- t.Fatal("Expected NewSamNode to fail when it cannot connect to the hub")- }- if !strings.Contains(err.Error(), "failed to authenticate with any hub") {- t.Fatalf("Expected 'failed to authenticate with any hub' error, got %v", err)- }-}-func TestStartRenewalLoop_ExpiredAndFails(t *testing.T) {if os.Getenv("BE_CRASHER") == "1" {store, _ := NewStore(t.TempDir())@@ -109,3 +93,28 @@ func TestStartRenewalLoop_ExpiredAndFails(t *testing.T) {}t.Fatalf("process ran with err %v, want exit status 1 (fatal crash)", err)}++func TestConnectionMonitor_CrashesAfterFailures(t *testing.T) {+ if os.Getenv("BE_CRASHER_MONITOR") == "1" {+ priv, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ store, _ := NewStore(t.TempDir())+ node, err := NewSamNode(context.Background(), priv, nil, nil, store, "test", "10s", []string{"/ip4/127.0.0.1/tcp/0"}, false, nil, 0, false)+ if err != nil {+ os.Exit(0) // Ignore NewSamNode errors for this crasher+ }++ // Use very short durations+ node.startConnectionMonitor(context.Background(), 10*time.Millisecond, 10*time.Millisecond, 3)+ time.Sleep(1 * time.Second)+ os.Exit(0) // should not be reached+ return+ }++ cmd := exec.Command(os.Args[0], "-test.run=TestConnectionMonitor_CrashesAfterFailures")+ cmd.Env = append(os.Environ(), "BE_CRASHER_MONITOR=1")+ err := cmd.Run()+ if e, ok := err.(*exec.ExitError); ok && !e.Success() {+ return // Successful fatal exit+ }+ t.Fatalf("process ran with err %v, want exit status 1 (fatal crash)", err)+}diff --git a/tests/e2e/auth_flows.bats b/tests/e2e/auth_flows.batsindex 50a4e8bd..e23325fe 100644--- a/tests/e2e/auth_flows.bats+++ b/tests/e2e/auth_flows.bats@@ -3,14 +3,6 @@load "lib/container_mesh.bash"setup() {- if ! mesh_require_docker; then- skip "docker not available or daemon not running"- fi-- if [[ ! -x "./bin/sam-node" || ! -x "./bin/sam-hub" ]]; then- skip "missing binaries; run: make build"- fi-mesh_setup_env}diff --git a/tests/e2e/watch.bats b/tests/e2e/watch.batsindex af11106a..99fdefd7 100644--- a/tests/e2e/watch.bats+++ b/tests/e2e/watch.bats@@ -2,16 +2,16 @@setup() {export SAM_NODE_BINARY="${SAM_NODE_BINARY:-./bin/sam-node}"- export SAM_HUB_BINARY="${SAM_HUB_BINARY:-./bin/sam-hub}"- if [[ ! -x "$SAM_NODE_BINARY" ]]; then- skip "sam-node binary not found at $SAM_NODE_BINARY"- fi+export TEST_TMPDIRTEST_TMPDIR="$(mktemp -d)"export HOME="$TEST_TMPDIR/home"- export SAM_DATA_DIR="$TEST_TMPDIR/data"- mkdir -p "$SAM_DATA_DIR"+ export XDG_CONFIG_HOME="$HOME/.config"+ mkdir -p "$XDG_CONFIG_HOME"++ # Generate mock DB+ go run tests/e2e/gen_db.go "$XDG_CONFIG_HOME/sam-mesh/agent.db"}teardown() {@@ -20,11 +20,8 @@ teardown() {}@test "sam-node run with stored identity fails if hub is unreachable" {- DB_PATH="$SAM_DATA_DIR/agent.db"- go run tests/e2e/gen_db.go "$DB_PATH"-log_file="$TEST_TMPDIR/run.log"- run "$SAM_NODE_BINARY" run --listen /ip4/127.0.0.1/udp/0/quic-v1 --listen /ip4/127.0.0.1/tcp/0 --api-token "dummy-token" --bind-addr "127.0.0.1:0" >"$log_file" 2>&1+ run "$SAM_NODE_BINARY" run --listen /ip4/127.0.0.1/udp/0/quic-v1 --listen /ip4/127.0.0.1/tcp/0 >"$log_file" 2>&1if [[ "$status" -eq 0 ]]; thenecho "Test failed: Node was expected to exit with non-zero status"@@ -38,45 +35,3 @@ teardown() {return 1fi}--@test "sam-node run with stored identity reaches online state when hub is reachable" {- if [[ ! -x "$SAM_HUB_BINARY" ]]; then- skip "sam-hub binary not found at $SAM_HUB_BINARY"- fi-- hub_log="$TEST_TMPDIR/hub.log"- "$SAM_HUB_BINARY" start >"$hub_log" 2>&1 &- hub_pid=$!-- # Wait for hub to start- sleep 2-- # Join node to the hub to get a valid stored identity- join_log="$TEST_TMPDIR/join.log"- "$SAM_NODE_BINARY" join "http://127.0.0.1:9090" >"$join_log" 2>&1-- log_file="$TEST_TMPDIR/run.log"- "$SAM_NODE_BINARY" run --listen /ip4/127.0.0.1/udp/0/quic-v1 --listen /ip4/127.0.0.1/tcp/0 --api-token "dummy-token" --bind-addr "127.0.0.1:0" >"$log_file" 2>&1 &- node_pid=$!-- online=""- for _ in {1..40}; do- if grep -q "SAM Node Online" "$log_file"; then- online="yes"- break- fi- sleep 0.1- done-- kill "$node_pid" >/dev/null 2>&1 || true- wait "$node_pid" >/dev/null 2>&1 || true- kill "$hub_pid" >/dev/null 2>&1 || true- wait "$hub_pid" >/dev/null 2>&1 || true-- if [[ "$online" != "yes" ]]; then- echo "Test failed. Node logs:"- cat "$log_file"- fi-- [[ "$online" == "yes" ]]-}diff --git a/tests/integration/proxy_test.go b/tests/integration/proxy_test.goindex 7c57a629..6a09a78a 100644--- a/tests/integration/proxy_test.go+++ b/tests/integration/proxy_test.go@@ -86,6 +86,7 @@ func TestSamNodeRunWithStoredIdentity(t *testing.T) {if err != context.DeadlineExceeded {t.Fatalf("expected run command to keep running until timeout, got: %v\nstdout:\n%s\nstderr:\n%s", err, runOut, runErrOut)}+ t.Logf("stdout:\n%s\nstderr:\n%s", runOut, runErrOut)out := runOut + runErrOutif !strings.Contains(out, "Using stored identity.") {