Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: swarm node agents never move out of pending (#2993)
Kyle Mendell3d1db1b16917
diff --git a/backend/internal/services/swarm_service.go b/backend/internal/services/swarm_service.goindex ca20b6451..026146504 100644--- a/backend/internal/services/swarm_service.go+++ b/backend/internal/services/swarm_service.go@@ -659,10 +659,17 @@ func (s *SwarmService) buildNodeAgentStatusInternal(nodeID string, env *models.Ereturn swarmtypes.NodeAgentStatus{State: swarmtypes.NodeAgentStateNone}}+ // An agent is live when it holds an open tunnel or has checked in via the poll+ // control plane recently. lastHeartbeat/lastPollAt are only populated from the+ // in-memory runtime registries when activity is fresh, so their presence is+ // current evidence — unlike env.Status, which poll-mode agents never advance+ // out of "pending" (HandlePoll updates the registry, not the persisted record).+ live := runtime.connected || runtime.lastHeartbeat != nil || runtime.lastPollAt != nil+status := swarmtypes.NodeAgentStatus{State: swarmtypes.NodeAgentStateOffline,EnvironmentID: &env.ID,- Connected: &runtime.connected,+ Connected: &live,LastHeartbeat: runtime.lastHeartbeat,LastPollAt: runtime.lastPollAt,}@@ -676,6 +683,8 @@ func (s *SwarmService) buildNodeAgentStatusInternal(nodeID string, env *models.E}}+ // A live tunnel with a completed identity probe lets us authoritatively detect+ // a node-ID mismatch (only tunnel mode can probe identity).if runtime.connected && runtime.identity != nil {if !runtime.identity.SwarmActive || strings.TrimSpace(runtime.identity.SwarmNodeID) != strings.TrimSpace(nodeID) {status.State = swarmtypes.NodeAgentStateMismatched@@ -685,7 +694,17 @@ func (s *SwarmService) buildNodeAgentStatusInternal(nodeID string, env *models.Ereturn status}- if env.Status == string(models.EnvironmentStatusPending) || (env.LastSeen == nil && runtime.lastHeartbeat == nil && runtime.lastPollAt == nil) {+ // Poll-mode agents (and tunnels without a completed probe) can't be identity-+ // verified live, but a fresh check-in proves the agent mapped to this node is+ // reachable. The node-to-env mapping was established by Arcane at deploy time,+ // so report it as connected instead of masking it behind a stale env.Status.+ if live {+ status.State = swarmtypes.NodeAgentStateConnected+ return status+ }++ // No live evidence: an unpaired agent stays pending; one seen before is offline.+ if env.Status == string(models.EnvironmentStatusPending) || env.LastSeen == nil {status.State = swarmtypes.NodeAgentStatePendingreturn status}diff --git a/backend/internal/services/swarm_service_test.go b/backend/internal/services/swarm_service_test.goindex ac4854b19..07e4219ee 100644--- a/backend/internal/services/swarm_service_test.go+++ b/backend/internal/services/swarm_service_test.go@@ -7,6 +7,7 @@ import ("net/http/httptest""path/filepath""testing"+ "time"cerrdefs "github.com/containerd/errdefs""github.com/getarcaneapp/arcane/backend/v2/internal/models"@@ -255,3 +256,58 @@ func TestSwarmService_ScaleService_HandlesServiceModesInternal(t *testing.T) {})}}++// TestSwarmService_BuildNodeAgentStatusInternal covers the state classification.+// The regression case is a poll-mode agent: its persisted env.Status never leaves+// "pending" (HandlePoll only updates the in-memory poll registry), so a fresh+// lastPollAt must still resolve to "connected" rather than "pending".+func TestSwarmService_BuildNodeAgentStatusInternal(t *testing.T) {+ const nodeID = "node-abc"+ now := time.Now()+ svc := &SwarmService{}++ tests := []struct {+ name string+ env *models.Environment+ runtime swarmNodeAgentRuntime+ want swarmtypes.NodeAgentState+ }{+ {+ name: "poll-mode check-in reports connected despite stale pending status",+ env: &models.Environment{Status: string(models.EnvironmentStatusPending)},+ runtime: swarmNodeAgentRuntime{lastPollAt: &now},+ want: swarmtypes.NodeAgentStateConnected,+ },+ {+ name: "no runtime activity and never paired stays pending",+ env: &models.Environment{Status: string(models.EnvironmentStatusPending)},+ runtime: swarmNodeAgentRuntime{},+ want: swarmtypes.NodeAgentStatePending,+ },+ {+ name: "tunnel with mismatched identity reports mismatched",+ env: &models.Environment{Status: string(models.EnvironmentStatusOnline)},+ runtime: swarmNodeAgentRuntime{connected: true, identity: &SwarmNodeIdentity{SwarmNodeID: "other-node", SwarmActive: true}},+ want: swarmtypes.NodeAgentStateMismatched,+ },+ {+ name: "tunnel connected without identity probe reports connected",+ env: &models.Environment{Status: string(models.EnvironmentStatusPending)},+ runtime: swarmNodeAgentRuntime{connected: true},+ want: swarmtypes.NodeAgentStateConnected,+ },+ {+ name: "previously seen agent with no activity reports offline",+ env: &models.Environment{Status: string(models.EnvironmentStatusOnline), LastSeen: &now},+ runtime: swarmNodeAgentRuntime{},+ want: swarmtypes.NodeAgentStateOffline,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := svc.buildNodeAgentStatusInternal(nodeID, tt.env, tt.runtime)+ require.Equal(t, tt.want, got.State)+ })+ }+}