Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
perf: reduce database chatter and simplify hot queries (#4239)
Kyle Mendellee8afa9497ba
diff --git a/backend/internal/activity/handler.go b/backend/internal/activity/handler.goindex 068834168..847a58029 100644--- a/backend/internal/activity/handler.go+++ b/backend/internal/activity/handler.go@@ -39,7 +39,7 @@ type ActivityHandler struct {type EnvironmentDependencies struct {ProxyJSONRequest handlerutil.RemoteJSONProxy- ListRemoteEnvironments func(context.Context) ([]environment.Environment, error)+ ListActiveRemoteEnvironments func(context.Context) ([]environment.Environment, error)GetActiveRemoteEnvironment func(string) mo.Option[environment.Environment]ProxyJSONRequestForEnvironment func(context.Context, environment.Environment, string, string, []byte, any) errorResolveEnvironmentName func(context.Context, string) string@@ -268,7 +268,8 @@ func (h *ActivityHandler) cancelRequestedByInternal(ctx context.Context, forwardfunc (h *ActivityHandler) RunLocalStreamProducer(ctx context.Context, limit int, events chan<- activitytypes.StreamEvent) {sendSnapshot := func() bool {activities, _, err := h.activityService.ListActivitiesPaginated(ctx, "0", pagination.QueryParams{- Limit: resolveActivityStreamLimitInternal(limit),+ Limit: resolveActivityStreamLimitInternal(limit),+ SkipCount: true,})if err != nil {if ctx.Err() == nil {@@ -328,7 +329,7 @@ func (h *ActivityHandler) RunLocalStreamProducer(ctx context.Context, limit int,func (h *ActivityHandler) RunRemoteStreamPollers(ctx context.Context, ps *authz.PermissionSet, limit int, events chan<- activitytypes.StreamEvent) {agg.ReconcilePollersByKey(ctx,func(ctx context.Context) ([]environment.Environment, error) {- environments, err := h.environment.ListRemoteEnvironments(ctx)+ environments, err := h.environment.ListActiveRemoteEnvironments(ctx)if err != nil {return nil, err}diff --git a/backend/internal/activity/service.go b/backend/internal/activity/service.goindex 14cfd982a..85946dece 100644--- a/backend/internal/activity/service.go+++ b/backend/internal/activity/service.go@@ -1112,7 +1112,7 @@ func (s *ActivityService) ListActivitiesPaginated(ctx context.Context, environmevar activities []Activityq := s.db.WithContext(ctx).Model(&Activity{}).Where("environment_id = ?", environmentID)- q = scopeJobActivityVisibilityInternal(ctx, q, authz.PermActivitiesRead)+ q = scopeJobActivityVisibilityInternal(ctx, q, environmentID, authz.PermActivitiesRead)if term := strings.TrimSpace(params.Search); term != "" {escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(term)@@ -1239,7 +1239,7 @@ func (s *ActivityService) DeleteHistory(ctx context.Context, environmentID strinvar deleted int64if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {- ids, err := findTerminalActivityIDsInternal(scopeJobActivityVisibilityInternal(ctx, tx.Where("environment_id = ?", environmentID), authz.PermActivitiesDelete))+ ids, err := findTerminalActivityIDsInternal(scopeJobActivityVisibilityInternal(ctx, tx.Where("environment_id = ?", environmentID), environmentID, authz.PermActivitiesDelete))if err != nil {return errors.WrapIf(err, "failed to find activity history")}diff --git a/backend/internal/activity/visibility.go b/backend/internal/activity/visibility.goindex 31e4feb7b..c5540495b 100644--- a/backend/internal/activity/visibility.go+++ b/backend/internal/activity/visibility.go@@ -1,6 +1,7 @@package activityimport (+ "cmp""context""github.com/getarcaneapp/arcane/backend/v2/internal/middleware"@@ -9,41 +10,21 @@ import ("gorm.io/gorm")-// Job summaries are stored on the manager but belong to their execution environment.-func scopeJobActivityVisibilityInternal(ctx context.Context, query *gorm.DB, permission string) *gorm.DB {+// Job summaries live in their target environment's environment_id column.+func scopeJobActivityVisibilityInternal(ctx context.Context, query *gorm.DB, environmentID, permission string) *gorm.DB {permissions, present := middleware.PermissionsFromContext(ctx)- if !present {+ if !present || permissions.Allows(permission, environmentID) {return query}- target := "json_extract(metadata, '$.environmentId')"- valid := "json_type(metadata, '$.environmentId') = 'text' AND " + target + " <> ''"- if query.Name() == "postgres" {- target = "CAST(metadata AS jsonb)->>'environmentId'"- valid = "jsonb_typeof(CAST(metadata AS jsonb)->'environmentId') = 'string' AND " + target + " <> ''"- }- if permissions.Allows(permission, "") {- return query.Where("(type <> ? OR ("+valid+"))", activitytypes.TypeJobRun)- }- if permissions == nil {- return query.Where("type <> ?", activitytypes.TypeJobRun)- }- allowed := make([]string, 0, len(permissions.PerEnv))- for environmentID := range permissions.PerEnv {- if permissions.Allows(permission, environmentID) {- allowed = append(allowed, environmentID)- }- }- if len(allowed) == 0 {- return query.Where("type <> ?", activitytypes.TypeJobRun)- }- return query.Where("(type <> ? OR ("+valid+" AND "+target+" IN ?))", activitytypes.TypeJobRun, allowed)+ return query.Where("type <> ?", activitytypes.TypeJobRun)}func canReadJobActivityInternal(ctx context.Context, item activitytypes.Activity) bool {if item.Type != activitytypes.TypeJobRun {return true}- environmentID, _ := item.Metadata["environmentId"].(string)+ metadataEnvironmentID, _ := item.Metadata["environmentId"].(string)+ environmentID := cmp.Or(item.EnvironmentID, metadataEnvironmentID)if environmentID == "" {return false}diff --git a/backend/internal/activity/visibility_test.go b/backend/internal/activity/visibility_test.goindex 976fc50ae..eb0b0073e 100644--- a/backend/internal/activity/visibility_test.go+++ b/backend/internal/activity/visibility_test.go@@ -16,11 +16,9 @@ import (func TestJobActivityVisibilityFiltersBeforePagination(t *testing.T) {db := setupActivityServiceTestDBInternal(t)service := NewActivityService(db, nil)- for _, target := range []string{"0", "allowed", "private"} {- require.NoError(t, db.Create(&Activity{BaseModel: database.BaseModel{ID: target}, EnvironmentID: "0", Type: activitytypes.TypeJobRun, Status: activitytypes.StatusQueued, StartedAt: time.Now(), Metadata: database.JSON{"environmentId": target}}).Error)- }- for _, metadata := range []database.JSON{nil, {"environmentId": 0}} {- require.NoError(t, db.Create(&Activity{EnvironmentID: "0", Type: activitytypes.TypeJobRun, Status: activitytypes.StatusQueued, StartedAt: time.Now(), Metadata: metadata}).Error)+ for _, environmentID := range []string{"0", "private"} {+ require.NoError(t, db.Create(&Activity{BaseModel: database.BaseModel{ID: environmentID + "-job"}, EnvironmentID: environmentID, Type: activitytypes.TypeJobRun, Status: activitytypes.StatusSuccess, StartedAt: time.Now(), Metadata: database.JSON{"environmentId": environmentID}}).Error)+ require.NoError(t, db.Create(&Activity{BaseModel: database.BaseModel{ID: environmentID + "-pull"}, EnvironmentID: environmentID, Type: activitytypes.TypeImagePull, Status: activitytypes.StatusSuccess, StartedAt: time.Now()}).Error)}permissions := authz.NewPermissionSet()permissions.PerEnv["0"] = map[string]struct{}{authz.PermActivitiesRead: {}}@@ -32,24 +30,29 @@ func TestJobActivityVisibilityFiltersBeforePagination(t *testing.T) {require.NoError(t, err)require.EqualValues(t, 2, response.TotalItems)require.Len(t, activities, 1)- require.NotEqual(t, "private", activities[0].ID)require.True(t, canReadJobActivityInternal(ctx, activities[0]))seen[activities[0].ID] = true}require.Len(t, seen, 2)- require.False(t, canReadJobActivityInternal(ctx, activitytypes.Activity{Type: activitytypes.TypeJobRun, EnvironmentID: "0", Metadata: map[string]any{"environmentId": "private"}}))+ activities, response, err := service.ListActivitiesPaginated(ctx, "private", pagination.QueryParams{Limit: 10})+ require.NoError(t, err)+ require.EqualValues(t, 1, response.TotalItems)+ require.Len(t, activities, 1)+ require.Equal(t, "private-pull", activities[0].ID)+ require.False(t, canReadJobActivityInternal(ctx, activitytypes.Activity{Type: activitytypes.TypeJobRun, EnvironmentID: "private", Metadata: map[string]any{"environmentId": "allowed"}}))+ require.True(t, canReadJobActivityInternal(ctx, activitytypes.Activity{Type: activitytypes.TypeJobRun, Metadata: map[string]any{"environmentId": "allowed"}}))privileged := context.WithValue(t.Context(), middleware.ContextKeyUserPermissions, authz.SudoPermissionSet())- activities, response, err := service.ListActivitiesPaginated(privileged, "0", pagination.QueryParams{Limit: 10})+ activities, response, err = service.ListActivitiesPaginated(privileged, "private", pagination.QueryParams{Limit: 10})require.NoError(t, err)- require.Len(t, activities, 3)- require.EqualValues(t, 3, response.TotalItems)- require.False(t, canReadJobActivityInternal(privileged, activitytypes.Activity{Type: activitytypes.TypeJobRun, EnvironmentID: "0"}))- require.NoError(t, db.Model(&Activity{}).Where("type = ?", activitytypes.TypeJobRun).Update("status", activitytypes.StatusSuccess).Error)+ require.Len(t, activities, 2)+ require.EqualValues(t, 2, response.TotalItems)+ require.False(t, canReadJobActivityInternal(privileged, activitytypes.Activity{Type: activitytypes.TypeJobRun}))permissions.PerEnv["0"][authz.PermActivitiesDelete] = struct{}{}- deleted, err := service.DeleteHistory(ctx, "0")+ deleted, err := service.DeleteHistory(ctx, "private")require.NoError(t, err)require.EqualValues(t, 1, deleted)- var retained Activity- require.NoError(t, db.First(&retained, "id = ?", "private").Error)- require.NoError(t, db.First(&Activity{}, "id = ?", "allowed").Error)+ require.NoError(t, db.First(&Activity{}, "id = ?", "private-job").Error)+ deleted, err = service.DeleteHistory(ctx, "0")+ require.NoError(t, err)+ require.EqualValues(t, 2, deleted)}diff --git a/backend/internal/auth/handler.go b/backend/internal/auth/handler.goindex d86ea0f96..f90d049ad 100644--- a/backend/internal/auth/handler.go+++ b/backend/internal/auth/handler.go@@ -295,16 +295,15 @@ func (h *AuthHandler) Logout(ctx context.Context, input *struct{}) (*LogoutOutpu}, nil}-// GetCurrentUser returns the currently authenticated user's information.-// Uses ToUserResponseDto (not the generic struct mapper) so the RBAC fields+// GetCurrentUser reads the current user from the database. Uses+// ToUserResponseDto (not the generic struct mapper) so the RBAC fields// (RoleAssignments, PermissionsByEnv) are resolved via RoleService.func (h *AuthHandler) GetCurrentUser(ctx context.Context, input *struct{}) (*handlerutil.Out[usertypes.User], error) {- userID, exists := middleware.GetUserIDFromContext(ctx)- if !exists {- return nil, huma.Error401Unauthorized("Not authenticated")+ currentUser, err := handlerutil.RequireUser(ctx)+ if err != nil {+ return nil, err}-- userModel, err := h.userService.GetUser(ctx, userID)+ userModel, err := h.userService.GetUser(ctx, currentUser.ID)if err != nil {return nil, huma.Error500InternalServerError("Failed to get user information")}@@ -490,6 +489,7 @@ func (h *AuthHandler) UpdateMyProfile(ctx context.Context, input *UpdateMyProfilif err != nil {return nil, huma.Error500InternalServerError("Failed to update user")}+ h.authService.InvalidateUserTokenCache(updated.ID)out, err := h.userService.ToUserResponseDto(ctx, *updated)if err != nil {@@ -552,6 +552,7 @@ func (h *AuthHandler) UploadMyAvatar(ctx context.Context, input *UploadMyAvatarIslog.ErrorContext(ctx, "Failed to save avatar", "user_id", currentUser.ID, "error", err)return nil, huma.Error500InternalServerError("failed to save avatar")}+ h.authService.InvalidateUserTokenCache(currentUser.ID)// Reload user so the response reflects the new AvatarURLupdatedUser, err := h.userService.GetUser(ctx, currentUser.ID)@@ -592,6 +593,7 @@ func (h *AuthHandler) DeleteMyAvatar(ctx context.Context, input *struct{}) (*hanslog.ErrorContext(ctx, "Failed to delete avatar", "user_id", currentUser.ID, "error", err)return nil, huma.Error500InternalServerError("failed to delete avatar")}+ h.authService.InvalidateUserTokenCache(currentUser.ID)updatedUser, err := h.userService.GetUser(ctx, currentUser.ID)if err != nil {diff --git a/backend/internal/auth/service.go b/backend/internal/auth/service.goindex 768b47236..58e41163f 100644--- a/backend/internal/auth/service.go+++ b/backend/internal/auth/service.go@@ -881,14 +881,8 @@ func (s *AuthService) ChangePassword(ctx context.Context, userID, currentPassworif _, err = s.userService.SetPasswordAndRevokeSessionsExcept(ctx, user, newPassword, currentSessionID); err != nil {return err}- keys := make([]string, 0)- s.tokenCache.Range(func(key string, entry verifiedTokenEntry) bool {- if entry.User.ID == userID && entry.SessionID != currentSessionID {- keys = append(keys, key)- }- return true- })- s.tokenCache.DeleteMany(keys)+ // The current session's cached user still carries the old password flags.+ s.InvalidateUserTokenCache(userID)return nil}diff --git a/backend/internal/bootstrap/router_bootstrap.go b/backend/internal/bootstrap/router_bootstrap.goindex 3604dfa2b..b392b83c1 100644--- a/backend/internal/bootstrap/router_bootstrap.go+++ b/backend/internal/bootstrap/router_bootstrap.go@@ -250,7 +250,7 @@ func newRouter(p RouterParams) (*echo.Echo, *edge.TunnelServer) {handlerAppCtx := handlerutil.NewActivityAppContext(ctx)envResolver := func(ctx context.Context, id string) (string, *string, bool, error) {- env, err := deps.Environment.Service().GetEnvironmentByID(ctx, id)+ env, err := deps.Environment.Service().GetEnvironmentByIDCached(ctx, id)if err != nil || env == nil {return "", nil, false, err}diff --git a/backend/internal/config/schema/schema.go b/backend/internal/config/schema/schema.goindex e3810d5f1..4563a254c 100644--- a/backend/internal/config/schema/schema.go+++ b/backend/internal/config/schema/schema.go@@ -130,6 +130,9 @@ var overrideDocRules = map[string]overrideDocRule{"featureVulnerabilityManagementEnabled": {note: "Disabling retains existing reports and scanner settings, allows active work to finish, and leaves standalone image patching available.",},+ "featureSwarmEnabled": {+ note: "An active swarm cluster keeps Swarm enabled regardless of this value.",+ },"vulnerabilityScanInterval": {requires: "FEATURE_VULNERABILITY_MANAGEMENT_ENABLED=true and VULNERABILITY_SCAN_ENABLED=true to have effect at runtime.",},diff --git a/backend/internal/config/schema/schema_test.go b/backend/internal/config/schema/schema_test.goindex 422cd4c3c..983e1dd69 100644--- a/backend/internal/config/schema/schema_test.go+++ b/backend/internal/config/schema/schema_test.go@@ -307,6 +307,7 @@ var expectedSettingOverrideKeys = []string{"eventCleanupInterval","experimentalFeaturesEnabled","expiredSessionsCleanupInterval",+ "featureSwarmEnabled","featureVulnerabilityManagementEnabled","followProjectSymlinks","gitOperationTimeout",diff --git a/backend/internal/dashboard/handler.go b/backend/internal/dashboard/handler.goindex 8d89cd7bb..a8d2b66ab 100644--- a/backend/internal/dashboard/handler.go+++ b/backend/internal/dashboard/handler.go@@ -168,7 +168,7 @@ func (h *DashboardHandler) RunLocalStreamProducer(ctx context.Context, debugAllGfunc (h *DashboardHandler) RunRemoteStreamPollers(ctx context.Context, ps *authz.PermissionSet, debugAllGood bool, events chan<- dashboardtypes.StreamEvent) {agg.ReconcilePollersByKey(ctx,func(ctx context.Context) ([]environment.Environment, error) {- environments, err := h.environmentService.ListRemoteEnvironments(ctx)+ environments, err := h.environmentService.ListActiveRemoteEnvironments(ctx)if err != nil {return nil, err}diff --git a/backend/internal/di/providers.go b/backend/internal/di/providers.goindex a799724ae..f494feb39 100644--- a/backend/internal/di/providers.go+++ b/backend/internal/di/providers.go@@ -69,7 +69,7 @@ func provideActivityModuleInternal(db *database.DB, settingsService *settings.SeSettings: settingsService,Environment: activity.EnvironmentDependencies{ProxyJSONRequest: environment.ProxyJSONRequest,- ListRemoteEnvironments: environment.ListRemoteEnvironments,+ ListActiveRemoteEnvironments: environment.ListActiveRemoteEnvironments,GetActiveRemoteEnvironment: environment.GetActiveRemoteEnvironmentSnapshot,ProxyJSONRequestForEnvironment: environment.ProxyJSONRequestForEnvironment,ResolveEnvironmentName: environment.ResolveEnvironmentName,@@ -428,13 +428,14 @@ func provideDashboardModuleInternal(db *database.DB, docker *docker.DockerClient})}-func provideSystemModuleInternal(db *database.DB, cfg *config.Config, docker *docker.DockerClientService, containerModule *container.Module, image *image.ImageService, volumeModule *volume.Module, network *network.NetworkService, settings *settings.SettingsService, activity *activity.ActivityService, upgrade *system.SystemUpgradeService, environment *environment.EnvironmentService) *system.Module {+func provideSystemModuleInternal(db *database.DB, cfg *config.Config, docker *docker.DockerClientService, containerModule *container.Module, image *image.ImageService, imageUpdate *imageupdate.ImageUpdateService, volumeModule *volume.Module, network *network.NetworkService, settings *settings.SettingsService, activity *activity.ActivityService, upgrade *system.SystemUpgradeService, environment *environment.EnvironmentService) *system.Module {return system.New(system.Dependencies{DB: db,Config: cfg,Docker: docker,Container: containerModule.Service(),Image: image,+ ImageUpdate: imageUpdate,Volume: volumeModule.Service(),Network: network,Settings: settings,diff --git a/backend/internal/environment/environment.go b/backend/internal/environment/environment.goindex 450a5d435..8b131206d 100644--- a/backend/internal/environment/environment.go+++ b/backend/internal/environment/environment.go@@ -25,21 +25,23 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils/validation"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity""github.com/getarcaneapp/arcane/types/v2/containerregistry"+ "github.com/samber/hot""go.getarcane.app/kit/normalization""go.getarcane.app/sys/crypto""gorm.io/gorm")type EnvironmentService struct {- db *database.DB- httpClient *http.Client- dockerService *docker.DockerClientService- eventService *event.EventService- settingsService *settings.SettingsService- apiKeyService *apikey.ApiKeyService- remoteClient *remenv.Client- edgeTokens *edgeTokenCacheInternal- remoteEnvs *remoteEnvSnapshotCacheInternal+ db *database.DB+ httpClient *http.Client+ dockerService *docker.DockerClientService+ eventService *event.EventService+ settingsService *settings.SettingsService+ apiKeyService *apikey.ApiKeyService+ remoteClient *remenv.Client+ edgeTokens *edgeTokenCacheInternal+ remoteEnvs *remoteEnvSnapshotCacheInternal+ environmentCache *hot.HotCache[string, Environment]// jobs carries the scheduler and app lifecycle context, injected// post-construction via SetScheduler (manager-only). Each enabled environment@@ -68,6 +70,7 @@ const (ErrEnvironmentAccessTokenRequired = errors.Sentinel("environment access token required")ErrInvalidEnvironmentAccessToken = errors.Sentinel("invalid environment access token")+ ErrEnvironmentNotFound = errors.Sentinel("environment not found"))// VariableSyncer pushes the effective global-variable set to one environment.@@ -102,9 +105,10 @@ func NewEnvironmentService(db *database.DB, httpClient *http.Client, dockerServiEnsureAvailableFunc: ensureRemoteEnvironmentTunnelAvailableInternal,DoFunc: doRemoteEnvironmentTunnelRequestInternal,}),- edgeTokens: newEdgeTokenCacheInternal(),- remoteEnvs: newRemoteEnvSnapshotCacheInternal(),- jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScopeInternal),+ edgeTokens: newEdgeTokenCacheInternal(),+ remoteEnvs: newRemoteEnvSnapshotCacheInternal(),+ environmentCache: newEnvironmentCacheInternal(),+ jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScopeInternal),}}@@ -184,7 +188,7 @@ func (s *EnvironmentService) ResolveEnvironmentName(ctx context.Context, environif strings.TrimSpace(environmentID) == "" {environmentID = LocalEnvironmentID}- env, err := s.GetEnvironmentByID(ctx, environmentID)+ env, err := s.GetEnvironmentByIDCached(ctx, environmentID)if err != nil || env == nil {if !errors.Is(err, context.Canceled) {slog.WarnContext(ctx, "failed to resolve environment name", "environmentID", environmentID, "error", err)@@ -204,6 +208,7 @@ func (s *EnvironmentService) EnsureLocalEnvironment(ctx context.Context, appUrlif err := s.db.WithContext(ctx).Model(&existingEnv).Update("api_url", appUrl).Error; err != nil {return errors.WrapIf(err, "failed to update local environment api url")}+ s.invalidateEnvironmentCacheInternal(LocalEnvironmentID)slog.InfoContext(ctx, "updated local environment api url", "id", LocalEnvironmentID, "url", appUrl)}return nil@@ -259,6 +264,8 @@ func (s *EnvironmentService) CreateEnvironment(ctx context.Context, environments.registerHealthJobInternal(ctx, environment.ID)}s.remoteEnvs.put(*environment)+ s.invalidateEnvironmentCacheInternal(environment.ID)+ s.NotifyRuntimeStateChanged()return environment, nil}@@ -267,7 +274,7 @@ func (s *EnvironmentService) GetEnvironmentByID(ctx context.Context, id string)var envRecord Environmentif err := s.db.WithContext(ctx).Where("id = ?", id).First(&envRecord).Error; err != nil {if errors.Is(err, gorm.ErrRecordNotFound) {- return nil, errors.New("environment not found")+ return nil, ErrEnvironmentNotFound}return nil, errors.WrapIf(err, "failed to get environment")}@@ -312,6 +319,8 @@ func (s *EnvironmentService) UpdateEnvironment(ctx context.Context, id string, uif err := s.db.WithContext(ctx).Model(&Environment{}).Where("id = ?", id).Updates(updates).Error; err != nil {return nil, errors.WrapIf(err, "failed to update environment")}+ s.invalidateEnvironmentCacheInternal(id)+ s.NotifyRuntimeStateChanged()updated, err := s.GetEnvironmentByID(ctx, id)if err != nil {@@ -396,6 +405,8 @@ func (s *EnvironmentService) DeleteEnvironment(ctx context.Context, id string, us.edgeTokens.invalidate(id)s.ForgetSyncState(id)s.remoteEnvs.remove(id)+ s.invalidateEnvironmentCacheInternal(id)+ s.NotifyRuntimeStateChanged()// Create event in backgroundgo s.createEnvironmentEvent(context.WithoutCancel(ctx), id, env.Name, event.EventTypeEnvironmentDelete, "Environment Deleted", fmt.Sprintf("Environment '%s' was deleted", env.Name), event.EventSeverityWarning, userID, username)@@ -443,8 +454,10 @@ func (s *EnvironmentService) RegenerateEnvironmentApiKey(ctx context.Context, enif result.RowsAffected == 0 {// A zero-row update would otherwise report a successful rotation while// the new key was never linked to anything.- return errors.New("environment not found")+ return ErrEnvironmentNotFound}+ s.invalidateEnvironmentCacheInternal(envID)+ s.NotifyRuntimeStateChanged()s.edgeTokens.sync(envID, apiKey)now := time.Now()diff --git a/backend/internal/environment/environment_caches.go b/backend/internal/environment/environment_caches.goindex bddb9a7ad..eb128c34b 100644--- a/backend/internal/environment/environment_caches.go+++ b/backend/internal/environment/environment_caches.go@@ -1,15 +1,22 @@package environmentimport (+ "cmp"+ "context"+ "slices""strings""sync""time"+ "emperror.dev/errors""github.com/samber/hot""github.com/samber/mo")-const edgeTokenCacheTTL = time.Minute+const (+ edgeTokenCacheTTL = time.Minute+ environmentCacheTTL = 30 * time.Second+)// edgeTokenCacheInternal maps an edge agent token to its environment ID. The// reverse index lets an environment's entry be dropped when its token rotates,@@ -23,8 +30,10 @@ type edgeTokenCacheInternal struct {// remoteEnvSnapshotCacheInternal holds the latest in-process copy of every// enabled, visible, non-local environment, so hot paths avoid a DB round trip.type remoteEnvSnapshotCacheInternal struct {- mu sync.RWMutex- envs map[string]Environment+ mu sync.RWMutex+ envs map[string]Environment+ seeded bool+ revision uint64}// runtimeWatchersInternal fans a coalesced wake-up out to everyone watching for@@ -45,6 +54,16 @@ func newEdgeTokenCacheInternal() *edgeTokenCacheInternal {}}+// newEnvironmentCacheInternal caches environment records by ID, including+// misses, for hot paths that only read CRUD-managed fields.+func newEnvironmentCacheInternal() *hot.HotCache[string, Environment] {+ return hot.NewHotCache[string, Environment](hot.LRU, 256).+ WithTTL(environmentCacheTTL).+ WithMissingSharedCache().+ WithJanitor().+ Build()+}+func newRemoteEnvSnapshotCacheInternal() *remoteEnvSnapshotCacheInternal {return &remoteEnvSnapshotCacheInternal{envs: make(map[string]Environment)}}@@ -133,23 +152,6 @@ func (c *remoteEnvSnapshotCacheInternal) get(environmentID string) mo.Option[Envreturn mo.Some(envRecord)}-func (c *remoteEnvSnapshotCacheInternal) replace(environments []Environment) {- if c == nil {- return- }-- next := make(map[string]Environment, len(environments))- for _, envRecord := range environments {- if isActiveRemoteEnvironmentInternal(envRecord) {- next[envRecord.ID] = envRecord- }- }-- c.mu.Lock()- c.envs = next- c.mu.Unlock()-}-func (c *remoteEnvSnapshotCacheInternal) put(environment Environment) {if c == nil {return@@ -162,6 +164,7 @@ func (c *remoteEnvSnapshotCacheInternal) put(environment Environment) {c.mu.Lock()c.envs[environment.ID] = environment+ c.revision++c.mu.Unlock()}@@ -172,6 +175,7 @@ func (c *remoteEnvSnapshotCacheInternal) remove(environmentID string) {c.mu.Lock()delete(c.envs, environmentID)+ c.revision++c.mu.Unlock()}@@ -182,6 +186,7 @@ func (c *remoteEnvSnapshotCacheInternal) update(environmentID string, update func.mu.Lock()defer c.mu.Unlock()+ c.revision++envRecord, ok := c.envs[environmentID]if !ok {@@ -203,3 +208,62 @@ func (s *EnvironmentService) GetActiveRemoteEnvironmentSnapshot(environmentID st}return s.remoteEnvs.get(environmentID)}++// ListActiveRemoteEnvironments returns every enabled, visible, non-local+// environment from memory, sorted by ID. The first call loads them from the+// database.+func (s *EnvironmentService) ListActiveRemoteEnvironments(ctx context.Context) ([]Environment, error) {+ s.remoteEnvs.mu.RLock()+ seeded := s.remoteEnvs.seeded+ environments := make([]Environment, 0, len(s.remoteEnvs.envs))+ for _, envRecord := range s.remoteEnvs.envs {+ environments = append(environments, envRecord)+ }+ s.remoteEnvs.mu.RUnlock()++ if !seeded {+ var err error+ if environments, err = s.ListRemoteEnvironments(ctx); err != nil {+ return nil, err+ }+ }++ slices.SortFunc(environments, func(a, b Environment) int {+ return cmp.Compare(a.ID, b.ID)+ })+ return environments, nil+}++// GetEnvironmentByIDCached is GetEnvironmentByID behind a short TTL cache that+// also remembers unknown IDs. Status and heartbeat fields may be stale.+func (s *EnvironmentService) GetEnvironmentByIDCached(ctx context.Context, id string) (*Environment, error) {+ envRecord, found, err := s.environmentCache.GetWithLoaders(id, func(ids []string) (map[string]Environment, error) {+ found := make(map[string]Environment, len(ids))+ for _, environmentID := range ids {+ loaded, loadErr := s.GetEnvironmentByID(ctx, environmentID)+ if errors.Is(loadErr, ErrEnvironmentNotFound) {+ continue+ }+ if loadErr != nil {+ return nil, loadErr+ }+ found[environmentID] = *loaded+ }+ return found, nil+ })+ if err != nil {+ return nil, err+ }+ if !found {+ return nil, ErrEnvironmentNotFound+ }+ return &envRecord, nil+}++// invalidateEnvironmentCacheInternal drops a cached record after a CRUD write.+func (s *EnvironmentService) invalidateEnvironmentCacheInternal(id string) {+ if s == nil || s.environmentCache == nil || id == "" {+ return+ }+ s.environmentCache.Delete(id)+}diff --git a/backend/internal/environment/environment_listing.go b/backend/internal/environment/environment_listing.goindex c7d4155ef..ceed35565 100644--- a/backend/internal/environment/environment_listing.go+++ b/backend/internal/environment/environment_listing.go@@ -187,6 +187,7 @@ func environmentTypeKeyInternal(env environment.Environment) string {}}+// ListVisibleEnvironments returns persisted rows; callers apply ApplyEnvironmentRuntimeState.func (s *EnvironmentService) ListVisibleEnvironments(ctx context.Context) ([]environment.Environment, error) {var envs []Environmentif err := s.db.WithContext(ctx).@@ -202,10 +203,6 @@ func (s *EnvironmentService) ListVisibleEnvironments(ctx context.Context) ([]envreturn nil, errors.WrapIf(mapErr, "failed to map environments")}- for i := range out {- ApplyEnvironmentRuntimeState(&out[i])- }-return out, nil}@@ -225,16 +222,35 @@ func (s *EnvironmentService) ListRemoteEnvironmentIDs(ctx context.Context) ([]st// ListRemoteEnvironments returns all non-local, enabled environments for syncing purposes.func (s *EnvironmentService) ListRemoteEnvironments(ctx context.Context) ([]Environment, error) {- var envs []Environment- err := s.db.WithContext(ctx).- Model(&Environment{}).- Where("id != ?", "0").- Where("enabled = ?", true).- Where("hidden = ?", false).- Find(&envs).Error- if err != nil {- return nil, errors.WrapIf(err, "failed to list remote environments")+ for {+ s.remoteEnvs.mu.RLock()+ revision := s.remoteEnvs.revision+ s.remoteEnvs.mu.RUnlock()++ var envs []Environment+ err := s.db.WithContext(ctx).+ Model(&Environment{}).+ Where("id != ?", "0").+ Where("enabled = ?", true).+ Where("hidden = ?", false).+ Find(&envs).Error+ if err != nil {+ return nil, errors.WrapIf(err, "failed to list remote environments")+ }++ s.remoteEnvs.mu.Lock()+ // Retry if a cache mutation or another refresh overtook the database read.+ if s.remoteEnvs.revision != revision {+ s.remoteEnvs.mu.Unlock()+ continue+ }+ s.remoteEnvs.envs = make(map[string]Environment, len(envs))+ for _, envRecord := range envs {+ s.remoteEnvs.envs[envRecord.ID] = envRecord+ }+ s.remoteEnvs.seeded = true+ s.remoteEnvs.revision+++ s.remoteEnvs.mu.Unlock()+ return envs, nil}- s.remoteEnvs.replace(envs)- return envs, nil}diff --git a/backend/internal/environment/environment_swarm_nodes.go b/backend/internal/environment/environment_swarm_nodes.goindex e5db1a770..8411476bf 100644--- a/backend/internal/environment/environment_swarm_nodes.go+++ b/backend/internal/environment/environment_swarm_nodes.go@@ -97,6 +97,8 @@ func (s *EnvironmentService) BindSwarmNodeEnvironment(}s.remoteEnvs.put(envRecord)+ s.invalidateEnvironmentCacheInternal(envRecord.ID)+ s.NotifyRuntimeStateChanged()return &envRecord, nil}@@ -108,6 +110,7 @@ func (s *EnvironmentService) DetachSwarmNodeEnvironment(ctx context.Context, parUpdates(map[string]any{"parent_environment_id": nil, "swarm_node_id": nil, "updated_at": &now}).Error; err != nil {return errors.WrapIf(err, "failed to detach swarm node environment")}+ s.NotifyRuntimeStateChanged()return nil}@@ -260,6 +263,7 @@ func (s *EnvironmentService) UpdateSwarmNodeIdentity(ctx context.Context, envID,if err := s.db.WithContext(ctx).Model(&Environment{}).Where("id = ?", envID).Updates(updates).Error; err != nil {return errors.WrapIf(err, "failed to update swarm node identity")}+ s.NotifyRuntimeStateChanged()return nil}diff --git a/backend/internal/environment/handler.go b/backend/internal/environment/handler.goindex 0a0a3bb20..ca055c628 100644--- a/backend/internal/environment/handler.go+++ b/backend/internal/environment/handler.go@@ -44,9 +44,11 @@ import (const (localDockerEnvironmentID = "0"- // Only covers poll-mode TTL expiry; tunnel and health-check changes arrive- // on the service's runtime-change signal instead.- environmentStreamPollInterval = 5 * time.Second+ // Re-applies in-memory runtime state to cached rows, covering poll-mode TTL+ // expiry; CRUD, tunnel and health-check changes arrive on the runtime-change signal.+ environmentStreamRuntimeInterval = 5 * time.Second+ // Safety net for writes that bypass the runtime-change signal.+ environmentStreamReloadInterval = 60 * time.SecondenvironmentStreamRefreshFloor = 30 * time.SecondenvironmentStreamHubKeyInternal = "environments")@@ -441,31 +443,53 @@ func (h *EnvironmentHandler) runEnvironmentStreamListerInternal(ctx context.Contchanges, unsubscribe := h.environmentService.SubscribeRuntimeChanges()defer unsubscribe()- list := func() {+ var rows []environment.Environment+ loaded := false+ publishRuntime := func() {+ if !loaded {+ return+ }+ envs := slices.Clone(rows)+ for i := range envs {+ ApplyEnvironmentRuntimeState(&envs[i])+ }+ publish(envs)+ }+ reload := func() {envs, err := h.environmentService.ListVisibleEnvironments(ctx)if err != nil {- // A failed read must not end the stream; the next tick retries.+ // A failed read must not end the stream; the next wake-up retries.if ctx.Err() == nil {slog.WarnContext(ctx, "environment stream failed to list environments", "error", err)}return}- publish(envs)+ rows = envs+ loaded = true+ publishRuntime()}- list()+ reload()- ticker := time.NewTicker(environmentStreamPollInterval)- defer ticker.Stop()+ runtimeTicker := time.NewTicker(environmentStreamRuntimeInterval)+ defer runtimeTicker.Stop()+ reloadTicker := time.NewTicker(environmentStreamReloadInterval)+ defer reloadTicker.Stop()for {select {case <-ctx.Done():returncase <-changes:- list()- case <-ticker.C:- list()+ reload()+ case <-reloadTicker.C:+ reload()+ case <-runtimeTicker.C:+ if !loaded {+ reload()+ continue+ }+ publishRuntime()}}}diff --git a/backend/internal/event/docker_correlation.go b/backend/internal/event/docker_correlation.goindex 307a53254..d231bed8c 100644--- a/backend/internal/event/docker_correlation.go+++ b/backend/internal/event/docker_correlation.go@@ -103,6 +103,14 @@ func (s *EventService) BeginDockerResourceSuppressionWindow(resourceType, resourreturn s.beginDockerWindowsInternal([]dockerWindowKeyInternal{{kind: "resource", resource: dockerExpectationInternal{resourceType, resourceID, resourceName}}})}+// BeginDockerTypeSuppressionWindow covers bulk daemon operations whose per-item IDs are unknown until they finish.+func (s *EventService) BeginDockerTypeSuppressionWindow(resourceType string) func() {+ if resourceType == "" {+ return func() {}+ }+ return s.beginDockerWindowsInternal([]dockerWindowKeyInternal{{kind: "type", name: resourceType}})+}+func (s *EventService) beginDockerWindowsInternal(keys []dockerWindowKeyInternal) func() {if s == nil {return func() {}@@ -177,6 +185,10 @@ func (s *EventService) ShouldSuppressDaemonEvent(resourceType, actorID, actorNamif composeProject == key.name {return true}+ case "type":+ if key.name == resourceType {+ return true+ }}}diff --git a/backend/internal/image/image.go b/backend/internal/image/image.goindex 006975d3c..16295de82 100644--- a/backend/internal/image/image.go+++ b/backend/internal/image/image.go@@ -37,7 +37,6 @@ import ("go.getarcane.app/updater""go.getarcane.app/updater/pkg/utils/tagpolicy""golang.org/x/sync/errgroup"- "gorm.io/gorm")type ImageService struct {@@ -165,25 +164,14 @@ func (s *ImageService) RemoveImage(ctx context.Context, id string, force bool, udefer s.eventService.BeginDockerResourceSuppressionWindow("image", "", tag)()}}- _, err = dockerClient.ImageRemove(ctx, id, options)+ removed, err := dockerClient.ImageRemove(ctx, id, options)if err != nil {s.eventService.LogErrorEvent(ctx, event.EventTypeImageError, "image", id, imageName, user.ID, user.Username, "0", err, database.JSON{"action": "delete", "force": force})return errors.WrapIf(err, "failed to remove image")}- if s.db != nil {- if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {- return tx.Delete(&imageupdate.ImageUpdateRecord{}, "id = ? OR image_id = ?", id, id).Error- }); err != nil {- slog.WarnContext(ctx, "failed to delete image update record", "id", id, "error", err)- }- }-- if s.imageUpdateService != nil {- if err := s.imageUpdateService.CleanupOrphanedRecords(ctx); err != nil {- slog.WarnContext(ctx, "failed to cleanup orphaned image update records after image remove", "id", id, "error", err)- }- }+ idsToDelete := append(getDeletedImageIDsInternal(removed.Items), id)+ s.cleanupImageUpdateRecordsInternal(ctx, idsToDelete)// Clean up vulnerability scan records for the deleted imageif s.vulnerabilityService != nil {@@ -570,10 +558,9 @@ func (s *ImageService) PruneImages(ctx context.Context, options systemtypes.Prun}pruneReport := report.Report- idsToDelete := getPrunedImageIDsInternal(pruneReport)- s.cleanupImageUpdateRecordsAfterPruneInternal(ctx, idsToDelete)+ idsToDelete := getDeletedImageIDsInternal(pruneReport.ImagesDeleted)+ s.cleanupImageUpdateRecordsInternal(ctx, idsToDelete)s.cleanupVulnerabilityRecordsAfterPruneInternal(ctx, idsToDelete)- s.cleanupOrphanedImageUpdatesAfterPruneInternal(ctx)metadata := database.JSON{"action": "prune",@@ -589,13 +576,13 @@ func (s *ImageService) PruneImages(ctx context.Context, options systemtypes.Prunreturn &pruneReport, nil}-func getPrunedImageIDsInternal(report image.PruneReport) []string {- if len(report.ImagesDeleted) == 0 {+func getDeletedImageIDsInternal(deletedImages []image.DeleteResponse) []string {+ if len(deletedImages) == 0 {return nil}- idsToDelete := make([]string, 0, len(report.ImagesDeleted))- for _, img := range report.ImagesDeleted {+ idsToDelete := make([]string, 0, len(deletedImages))+ for _, img := range deletedImages {if img.Deleted != "" {idsToDelete = append(idsToDelete, img.Deleted)continue@@ -607,18 +594,6 @@ func getPrunedImageIDsInternal(report image.PruneReport) []string {return idsToDelete}-func (s *ImageService) cleanupImageUpdateRecordsAfterPruneInternal(ctx context.Context, idsToDelete []string) {- if s.db == nil || len(idsToDelete) == 0 {- return- }-- if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {- return tx.Where("id IN ? OR image_id IN ?", idsToDelete, idsToDelete).Delete(&imageupdate.ImageUpdateRecord{}).Error- }); err != nil {- slog.WarnContext(ctx, "failed to clean up image update records after prune", "error", err)- }-}-func (s *ImageService) cleanupVulnerabilityRecordsAfterPruneInternal(ctx context.Context, idsToDelete []string) {if s.vulnerabilityService == nil || len(idsToDelete) == 0 {return@@ -629,13 +604,16 @@ func (s *ImageService) cleanupVulnerabilityRecordsAfterPruneInternal(ctx context}}-func (s *ImageService) cleanupOrphanedImageUpdatesAfterPruneInternal(ctx context.Context) {+func (s *ImageService) cleanupImageUpdateRecordsInternal(ctx context.Context, idsToDelete []string) {if s.imageUpdateService == nil {return}+ if err := s.imageUpdateService.DeleteRecordsForImages(ctx, idsToDelete); err != nil {+ slog.WarnContext(ctx, "failed to clean up image update records", "error", err)+ }if err := s.imageUpdateService.CleanupOrphanedRecords(ctx); err != nil {- slog.WarnContext(ctx, "failed to cleanup orphaned image update records after prune", "error", err)+ slog.WarnContext(ctx, "failed to clean up orphaned image update records", "error", err)}}diff --git a/backend/internal/imageupdate/image_update.go b/backend/internal/imageupdate/image_update.goindex 358cd8a69..5ede51b22 100644--- a/backend/internal/imageupdate/image_update.go+++ b/backend/internal/imageupdate/image_update.go@@ -1784,58 +1784,63 @@ func (s *ImageUpdateService) CheckAllImages(ctx context.Context, limit int, extereturn results, nil}+// DeleteRecordsForImages removes image-level rows (keyed by id) and+// container/project rows (keyed by image_id) for the given images.+func (s *ImageUpdateService) DeleteRecordsForImages(ctx context.Context, imageIDs []string) error {+ if s == nil || s.db == nil || len(imageIDs) == 0 {+ return nil+ }+ if err := s.db.WithContext(ctx).Where("id IN ? OR image_id IN ?", imageIDs, imageIDs).Delete(&ImageUpdateRecord{}).Error; err != nil {+ return errors.WrapIf(err, "failed to delete image update records")+ }+ return nil+}+func (s *ImageUpdateService) CleanupOrphanedRecords(ctx context.Context) error {if s.db == nil {return nil}-- dockerClient, err := s.dockerClientInternal(ctx)- if err != nil {- return err+ if s.dockerService == nil {+ return errors.New("docker service unavailable")}- // Get all image IDs from Docker- apiCtx, cancel := s.dockerAPIContextInternal(ctx)- defer cancel()-- dockerImagesResult, err := dockerClient.ImageList(apiCtx, client.ImageListOptions{})+ dockerImages, err := s.dockerService.ListImages(ctx)if err != nil {- return errors.WrapIf(err, "failed to list Docker images")+ return err}- dockerImages := dockerImagesResult.Items-dockerImageIDs := make([]string, 0, len(dockerImages))for _, img := range dockerImages {dockerImageIDs = append(dockerImageIDs, img.ID)}- var result *gorm.DB- if len(dockerImageIDs) == 0 {- result = s.db.WithContext(ctx).Where("container_id = ? AND project_id = ?", "", "").Delete(&ImageUpdateRecord{})- } else {- result = s.db.WithContext(ctx).Where("container_id = ? AND project_id = ? AND id NOT IN ?", "", "", dockerImageIDs).Delete(&ImageUpdateRecord{})+ imageScoped := s.db.WithContext(ctx).Where("container_id = ? AND project_id = ?", "", "")+ if len(dockerImageIDs) > 0 {+ imageScoped = imageScoped.Where("id NOT IN ?", dockerImageIDs)}- if result.Error != nil {- return errors.WrapIf(result.Error, "failed to delete orphaned records")+ imageResult := imageScoped.Delete(&ImageUpdateRecord{})+ if imageResult.Error != nil {+ return errors.WrapIf(imageResult.Error, "failed to delete orphaned records")}- containers, listErr := dockerClient.ContainerList(apiCtx, client.ContainerListOptions{All: true})- if listErr != nil {- return listErr+ containers, err := s.dockerService.ListContainers(ctx)+ if err != nil {+ return err}- ids := make([]string, 0, len(containers.Items))- for _, cnt := range containers.Items {- ids = append(ids, cnt.ID)+ containerIDs := make([]string, 0, len(containers))+ for _, cnt := range containers {+ containerIDs = append(containerIDs, cnt.ID)}- scoped := s.db.WithContext(ctx).Where("container_id <> ?", "")- if len(ids) > 0 {- scoped = scoped.Where("container_id NOT IN ?", ids)+ containerScoped := s.db.WithContext(ctx).Where("container_id <> ?", "")+ if len(containerIDs) > 0 {+ containerScoped = containerScoped.Where("container_id NOT IN ?", containerIDs)}- if err := scoped.Delete(&ImageUpdateRecord{}).Error; err != nil {- return err+ containerResult := containerScoped.Delete(&ImageUpdateRecord{})+ if containerResult.Error != nil {+ return errors.WrapIf(containerResult.Error, "failed to delete orphaned container records")}- if result.RowsAffected > 0 {- slog.InfoContext(ctx, "Cleaned up orphaned image update records", "deletedCount", result.RowsAffected)++ if deleted := imageResult.RowsAffected + containerResult.RowsAffected; deleted > 0 {+ slog.InfoContext(ctx, "Cleaned up orphaned image update records", "deletedCount", deleted)} else {slog.InfoContext(ctx, "No orphaned image update records found")}diff --git a/backend/internal/settings/features_test.go b/backend/internal/settings/features_test.goindex 40a0f3fbd..7f0951f23 100644--- a/backend/internal/settings/features_test.go+++ b/backend/internal/settings/features_test.go@@ -99,3 +99,22 @@ func TestSettingsService_RejectInvalidFeatureBoolean(t *testing.T) {})}}++func TestSettingsService_SwarmFeatureDefaultsOffAndPersists(t *testing.T) {+ var nilService *SettingsService+ require.False(t, nilService.IsFeatureEnabled(t.Context(), features.Swarm))++ svc, err := newSettingsServiceForTestInternal(t, t.Context(), setupSettingsTestDB(t))+ require.NoError(t, err)+ require.False(t, svc.IsFeatureEnabled(t.Context(), features.Swarm))+ require.Contains(t, svc.ListSettings(SettingVisibilityPublic), SettingVariable{Key: features.SwarmSettingKey, Value: "false"})++ enabled := "true"+ _, err = svc.UpdateSettings(t.Context(), settingstypes.Update{FeatureSwarmEnabled: &enabled})+ require.NoError(t, err)+ require.True(t, svc.IsFeatureEnabled(t.Context(), features.Swarm))++ invalid := "yes"+ _, err = svc.UpdateSettings(t.Context(), settingstypes.Update{FeatureSwarmEnabled: &invalid})+ require.ErrorIs(t, err, common.ErrValidation)+}diff --git a/backend/internal/settings/model.go b/backend/internal/settings/model.goindex 6678dbdbf..0f01433b4 100644--- a/backend/internal/settings/model.go+++ b/backend/internal/settings/model.go@@ -125,6 +125,7 @@ type Settings struct {AuthSessionTimeout SettingVariable `key:"authSessionTimeout" meta:"label=Session Timeout;type=number;keywords=session,timeout,expire,duration,lifetime,minutes,logout;category=authentication;description=How long user sessions remain active"`AuthPasswordPolicy SettingVariable `key:"authPasswordPolicy" meta:"label=Password Policy;type=select;keywords=password,policy,strength,complexity,requirements,security,rules;category=authentication;description=Set password strength requirements"`FeatureVulnerabilityManagementEnabled SettingVariable `key:"featureVulnerabilityManagementEnabled,public,envOverride" meta:"label=Vulnerability management;type=boolean;keywords=features,vulnerability,security,scan,enable,disable;category=general;description=Enable vulnerability management for this environment. Disabling retains reports and configuration and leaves standalone image patching available"`+ FeatureSwarmEnabled SettingVariable `key:"featureSwarmEnabled,public,envOverride" meta:"label=Docker Swarm;type=boolean;keywords=features,swarm,cluster,stacks,services,nodes,enable,disable;category=general;description=Show Docker Swarm for this environment. An active swarm cluster keeps it enabled"`VulnerabilityScanEnabled SettingVariable `key:"vulnerabilityScanEnabled" meta:"label=Scheduled Vulnerability Scan;type=boolean;keywords=vulnerability,scan,security,trivy,schedule,automatic,cve;category=security;description=Enable scheduled vulnerability scanning of all Docker images" catmeta:"id=security;title=Security;icon=shield;url=/settings/security;description=Configure vulnerability scanning and runtime security settings"`VulnerabilityScanInterval SettingVariable `key:"vulnerabilityScanInterval" meta:"label=Vulnerability Scan Interval;type=cron;keywords=vulnerability,scan,interval,schedule,frequency,trivy,cve;category=security;description=How often to run scheduled vulnerability scans (cron expression)"`VulnerabilityThreatIntelEnabled SettingVariable `key:"vulnerabilityThreatIntelEnabled,envOverride" meta:"label=Threat Intelligence;type=boolean;keywords=kev,cisa,epss,first,exploit,threat,intel,risk,airgap,offline,security;category=security;description=Fetch the CISA KEV catalog and FIRST EPSS scores to prioritize exploited vulnerabilities. Disable for air-gapped installs."`diff --git a/backend/internal/settings/service.go b/backend/internal/settings/service.goindex c7e90ab37..4de0607b3 100644--- a/backend/internal/settings/service.go+++ b/backend/internal/settings/service.go@@ -247,6 +247,7 @@ func DefaultSettingsConfig() *Settings {AuthSessionTimeout: SettingVariable{Value: "1440"},AuthPasswordPolicy: SettingVariable{Value: "strong"},FeatureVulnerabilityManagementEnabled: SettingVariable{Value: "true"},+ FeatureSwarmEnabled: SettingVariable{Value: "false"},VulnerabilityScanEnabled: SettingVariable{Value: "false"},VulnerabilityScanInterval: SettingVariable{Value: "0 0 0 * * *"},VulnerabilityThreatIntelEnabled: SettingVariable{Value: "true"},diff --git a/backend/internal/system/module.go b/backend/internal/system/module.goindex 4872c0588..5bccac71e 100644--- a/backend/internal/system/module.go+++ b/backend/internal/system/module.go@@ -11,6 +11,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/docker""github.com/getarcaneapp/arcane/backend/v2/internal/environment""github.com/getarcaneapp/arcane/backend/v2/internal/image"+ "github.com/getarcaneapp/arcane/backend/v2/internal/imageupdate""github.com/getarcaneapp/arcane/backend/v2/internal/network""github.com/getarcaneapp/arcane/backend/v2/internal/settings""github.com/getarcaneapp/arcane/backend/v2/internal/volume"@@ -24,6 +25,7 @@ type Dependencies struct {Docker *docker.DockerClientServiceContainer *container.ContainerServiceImage *image.ImageService+ ImageUpdate *imageupdate.ImageUpdateServiceVolume *volume.VolumeServiceNetwork *network.NetworkServiceSettings *settings.SettingsService@@ -46,6 +48,7 @@ func New(deps Dependencies) *Module {deps.Docker,deps.Container,deps.Image,+ deps.ImageUpdate,deps.Volume,deps.Network,deps.Settings,diff --git a/backend/internal/system/system.go b/backend/internal/system/system.goindex 296b1e1d6..7f6aae3c1 100644--- a/backend/internal/system/system.go+++ b/backend/internal/system/system.go@@ -39,6 +39,7 @@ type SystemService struct {dockerService *docker.DockerClientServicecontainerService *container.ContainerServiceimageService *image.ImageService+ imageUpdateService *imageupdate.ImageUpdateServicevolumeService *volume.VolumeServicenetworkService *network.NetworkServicesettingsService *settings.SettingsService@@ -53,21 +54,23 @@ func NewSystemService(dockerService *docker.DockerClientService,containerService *container.ContainerService,imageService *image.ImageService,+ imageUpdateService *imageupdate.ImageUpdateService,volumeService *volume.VolumeService,networkService *network.NetworkService,settingsService *settings.SettingsService,activityService *activity.ActivityService,) *SystemService {return &SystemService{- db: db,- dockerService: dockerService,- containerService: containerService,- imageService: imageService,- volumeService: volumeService,- networkService: networkService,- settingsService: settingsService,- activityService: activityService,- runningPrunes: make(map[string]string),+ db: db,+ dockerService: dockerService,+ containerService: containerService,+ imageService: imageService,+ imageUpdateService: imageUpdateService,+ volumeService: volumeService,+ networkService: networkService,+ settingsService: settingsService,+ activityService: activityService,+ runningPrunes: make(map[string]string),dockerHostMemoryCache: hot.NewHotCache[struct{}, dockerHostMemoryInfo](hot.LRU, 1).WithTTL(dockerHostMemoryCacheTTL).Build(),@@ -553,11 +556,8 @@ func (s *SystemService) pruneImagesInternal(ctx context.Context, options system.}}- // Batch delete update records- if len(idsToDelete) > 0 && s.db != nil {- if err := s.db.WithContext(ctx).Where("id IN ?", idsToDelete).Delete(&imageupdate.ImageUpdateRecord{}).Error; err != nil {- slog.WarnContext(ctx, "Failed to delete image update records", "count", len(idsToDelete), "error", err.Error())- }+ if err := s.imageUpdateService.DeleteRecordsForImages(ctx, idsToDelete); err != nil {+ slog.WarnContext(ctx, "Failed to delete image update records", "count", len(idsToDelete), "error", err.Error())}result.ImagesDeleted = idsToDeletediff --git a/backend/internal/user/service.go b/backend/internal/user/service.goindex 0e745a21e..f589f987a 100644--- a/backend/internal/user/service.go+++ b/backend/internal/user/service.go@@ -599,20 +599,39 @@ func (s *UserService) ListUsersPaginated(ctx context.Context, params pagination.}func (s *UserService) ToUserResponseDto(ctx context.Context, u common.User) (user.User, error) {- return s.toUserResponseDtoInternal(ctx, u), nil+ dto := s.toUserResponseDtoInternal(ctx, u)+ if dto.IsGlobalAdmin && s.roleService != nil {+ remaining, err := s.roleService.CountGlobalAdminsExcludingUser(ctx, u.ID)+ if err != nil {+ return user.User{}, errors.WrapIf(err, "failed to check whether user can be deleted")+ }+ dto.CanDelete = remaining > 0+ }+ return dto, nil}+// toUserResponseDtosInternal maps a user list. CanDelete is false on the only+// effective global admin.func (s *UserService) toUserResponseDtosInternal(ctx context.Context, users []common.User) []user.User {result := make([]user.User, len(users))for i, u := range users {result[i] = s.toUserResponseDtoInternal(ctx, u)}+ if s.roleService == nil {+ return result+ }+ if total, err := s.roleService.CountGlobalAdminsExcludingUser(ctx, ""); err == nil && total == 1 {+ for i := range result {+ if result[i].IsGlobalAdmin {+ result[i].CanDelete = false+ }+ }+ }return result}// toUserResponseDtoInternal builds the public User DTO. RoleAssignments and-// PermissionsByEnv come from the RBAC service. CanDelete is false when this-// user is the only effective global admin.+// PermissionsByEnv come from the RBAC service. Callers apply the last-admin guard.func (s *UserService) toUserResponseDtoInternal(ctx context.Context, u common.User) user.User {dto := user.User{ID: u.ID,@@ -656,11 +675,6 @@ func (s *UserService) toUserResponseDtoInternal(ctx context.Context, u common.Usif ps, err := s.roleService.ResolvePermissions(ctx, &u); err == nil && ps != nil {dto.IsGlobalAdmin = ps.IsGlobalAdmin()dto.PermissionsByEnv = permissionSetToMap(ps)- if dto.IsGlobalAdmin {- if remaining, cerr := s.roleService.CountGlobalAdminsExcludingUser(ctx, u.ID); cerr == nil && remaining == 0 {- dto.CanDelete = false- }- }}return dto}diff --git a/backend/internal/user/service_test.go b/backend/internal/user/service_test.goindex e192197df..51e0e6ee7 100644--- a/backend/internal/user/service_test.go+++ b/backend/internal/user/service_test.go@@ -163,6 +163,19 @@ func TestListUsersPaginatedSetsCanDeleteFromGlobalAdminCount(t *testing.T) {require.False(t, canDeleteByID[lastAdmin.ID])require.True(t, canDeleteByID[nonAdmin.ID])++ adminDTO, err := userSvc.ToUserResponseDto(ctx, *lastAdmin)+ require.NoError(t, err)+ require.False(t, adminDTO.CanDelete)+ userDTO, err := userSvc.ToUserResponseDto(ctx, *nonAdmin)+ require.NoError(t, err)+ require.True(t, userDTO.CanDelete)++ backup := createTestUser(t, userSvc, "admin-2", "backup")+ grantGlobalAdmin(t, roleSvc, backup.ID)+ adminDTO, err = userSvc.ToUserResponseDto(ctx, *lastAdmin)+ require.NoError(t, err)+ require.True(t, adminDTO.CanDelete)}func TestDeleteUserRejectsDeletingOnlyCustomAllPermissionsAdmin(t *testing.T) {@@ -188,6 +201,9 @@ func TestDeleteUserRejectsDeletingOnlyCustomAllPermissionsAdmin(t *testing.T) {require.Len(t, users, 1)require.False(t, users[0].CanDelete)require.True(t, users[0].IsGlobalAdmin)+ dto, err := userSvc.ToUserResponseDto(ctx, *customAdmin)+ require.NoError(t, err)+ require.False(t, dto.CanDelete)}func TestUserProfileValidationAndPersistence(t *testing.T) {diff --git a/backend/internal/vulnerability/vulnerability_store.go b/backend/internal/vulnerability/vulnerability_store.goindex 722fe652b..985a396ca 100644--- a/backend/internal/vulnerability/vulnerability_store.go+++ b/backend/internal/vulnerability/vulnerability_store.go@@ -35,6 +35,12 @@ func (s *VulnerabilityService) deleteScanChildRowsInternal(ctx context.Context,slog.WarnContext(ctx, "failed to list stored vulnerability reports for cleanup", "error", err)return}+ s.deleteScanChildRowsByImageIDsInternal(ctx, imageIDs)+}++// deleteScanChildRowsByImageIDsInternal is deleteScanChildRowsInternal for+// callers that already hold the image IDs, which are the scan record IDs.+func (s *VulnerabilityService) deleteScanChildRowsByImageIDsInternal(ctx context.Context, imageIDs []string) {if len(imageIDs) == 0 {return}@@ -219,7 +225,7 @@ func (s *VulnerabilityService) DeleteScanResult(ctx context.Context, imageID strreturn nil}- s.deleteScanChildRowsInternal(ctx, s.db.WithContext(ctx).Model(&VulnerabilityScanRecord{}).Where("id = ?", imageID))+ s.deleteScanChildRowsByImageIDsInternal(ctx, []string{imageID})return s.db.WithContext(ctx).Where("id = ?", imageID).Delete(&VulnerabilityScanRecord{}).Error}@@ -229,7 +235,7 @@ func (s *VulnerabilityService) DeleteScanResultsByImageIDs(ctx context.Context,return nil}- s.deleteScanChildRowsInternal(ctx, s.db.WithContext(ctx).Model(&VulnerabilityScanRecord{}).Where("id IN ?", imageIDs))+ s.deleteScanChildRowsByImageIDsInternal(ctx, imageIDs)return s.db.WithContext(ctx).Where("id IN ?", imageIDs).Delete(&VulnerabilityScanRecord{}).Error}diff --git a/backend/pkg/pagination/reflect.go b/backend/pkg/pagination/reflect.goindex ba5db5e20..587a8749d 100644--- a/backend/pkg/pagination/reflect.go+++ b/backend/pkg/pagination/reflect.go@@ -4,6 +4,7 @@ import ("cmp""reflect""strconv"+ "strings"kit "go.getarcane.app/kit/pkg""gorm.io/gorm"@@ -33,8 +34,8 @@ func PaginateAndSortDB[M any](params QueryParams, query *gorm.DB, result *[]M) (// Without a total order the page cut is undefined: rows can repeat or go// missing across pages when the requested column has ties (or no sort was// requested at all) and the database reorders a plain scan. Tie-break on the- // primary key whenever the model has one.- if _, hasID := modelType.FieldByName("ID"); hasID && columnName != "id" {+ // primary key whenever the model has one and the caller did not already.+ if _, hasID := modelType.FieldByName("ID"); hasID && columnName != "id" && !orderedByIDInternal(query) {orderColumns = append(orderColumns, clause.OrderByColumn{Column: clause.Column{Table: clause.CurrentTable, Name: "id"},})@@ -46,7 +47,7 @@ func PaginateAndSortDB[M any](params QueryParams, query *gorm.DB, result *[]M) (limit := params.Limit// limit = -1 means "show all" - skip paginationif limit == -1 {- return paginateDBAll(query, result, params.SkipCount)+ return paginateDBAll(query, result)}if limit <= 0 {limit = 20@@ -61,35 +62,45 @@ func PaginateAndSortDB[M any](params QueryParams, query *gorm.DB, result *[]M) (return paginateDB(params.Start, limit, query, result, params.SkipCount)}-// paginateDBAll returns all results without pagination limits.-// When skipCount is true the COUNT(*) is elided and TotalItems/TotalPages are returned as UnknownTotal.-// Count runs before Find so it sees a clean session (Find sets Statement.Dest in GORM v2).-func paginateDBAll[M any](query *gorm.DB, result *[]M, skipCount bool) (Response, error) {- var totalItems int64- if !skipCount {- if err := query.Count(&totalItems).Error; err != nil {- return Response{}, err+// orderedByIDInternal reports whether the query's existing ORDER BY already includes the id column.+func orderedByIDInternal(query *gorm.DB) bool {+ existing, ok := query.Statement.Clauses["ORDER BY"]+ if !ok {+ return false+ }+ orderBy, ok := existing.Expression.(clause.OrderBy)+ if !ok {+ return false+ }+ for _, column := range orderBy.Columns {+ name := column.Column.Name+ if column.Column.Raw {+ fields := strings.Fields(name)+ if len(fields) == 0 {+ continue+ }+ name = fields[0]+ }+ name = strings.Trim(name[strings.LastIndex(name, ".")+1:], "`\"")+ if strings.EqualFold(name, "id") || name == clause.PrimaryKey {+ return true}}+ return false+}+// paginateDBAll returns all results without pagination limits or a COUNT(*).+func paginateDBAll[M any](query *gorm.DB, result *[]M) (Response, error) {if err := query.Find(result).Error; err != nil {return Response{}, err}- if skipCount {- return Response{- TotalPages: UnknownTotal,- TotalItems: UnknownTotal,- CurrentPage: 1,- ItemsPerPage: 0,- }, nil- }-+ totalItems := len(*result)return Response{TotalPages: 1,- TotalItems: totalItems,+ TotalItems: int64(totalItems),CurrentPage: 1,- ItemsPerPage: int(totalItems),+ ItemsPerPage: totalItems,}, nil}diff --git a/backend/pkg/pagination/reflect_test.go b/backend/pkg/pagination/reflect_test.goindex 78ea4e7f1..ea4893c0a 100644--- a/backend/pkg/pagination/reflect_test.go+++ b/backend/pkg/pagination/reflect_test.go@@ -56,3 +56,17 @@ func TestPaginateAndSortDB_SortTiesBrokenByID(t *testing.T) {require.Equal(t, []string{"a", "b", "c", "d", "e"}, walkWidgetPages(t, db, "name"))}++func TestPaginateAndSortDB_CallerIDOrderSkipsTieBreak(t *testing.T) {+ db := newTieBreakTestDB(t)++ require.True(t, orderedByIDInternal(db.Model(&widget{}).Order("name").Order("widgets.id DESC")))+ require.True(t, orderedByIDInternal(db.Model(&widget{}).Order("`id` DESC")))+ require.False(t, orderedByIDInternal(db.Model(&widget{}).Order("name DESC")))+ require.False(t, orderedByIDInternal(db.Model(&widget{})))++ var got []widget+ _, err := PaginateAndSortDB(QueryParams{Limit: 5}, db.Model(&widget{}).Order("id DESC"), &got)+ require.NoError(t, err)+ require.Equal(t, "e", got[0].ID)+}diff --git a/backend/pkg/pagination/skipcount_test.go b/backend/pkg/pagination/skipcount_test.goindex 83576f569..881ca7c50 100644--- a/backend/pkg/pagination/skipcount_test.go+++ b/backend/pkg/pagination/skipcount_test.go@@ -65,8 +65,8 @@ func TestPaginateAndSortDB_SkipCountShowAll(t *testing.T) {}, db.Model(&widget{}), &got)require.NoError(t, err)require.Len(t, got, 5)- require.Equal(t, UnknownTotal, resp.TotalItems)- require.Equal(t, UnknownTotal, resp.TotalPages)+ require.Equal(t, int64(5), resp.TotalItems)+ require.Equal(t, int64(1), resp.TotalPages)}// A start that does not land on a page boundary used to be rounded down to thediff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex 2bb97922c..acd721f41 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -3755,6 +3755,9 @@"features_unsupported": "Upgrade this environment’s Arcane agent to manage feature availability. Its existing behavior is unchanged.","features_unavailable": "Feature settings are unavailable. Check the environment connection and try again.","features_environment_override": "This feature setting is managed by an environment variable. Change FEATURE_VULNERABILITY_MANAGEMENT_ENABLED on this environment to update it.",+ "features_swarm_description": "Show Swarm services, stacks, nodes, and cluster management for this environment. Turning this off hides Swarm from navigation.",+ "features_swarm_locked": "This environment is part of an active swarm cluster, so Swarm stays enabled. Leave the swarm to turn it off.",+ "features_swarm_environment_override": "This feature setting is managed by an environment variable. Change FEATURE_SWARM_ENABLED on this environment to update it.","history": "History","destination": "Destination","changes": "Changes",diff --git a/frontend/src/lib/components/mobile-nav/mobile-nav.svelte b/frontend/src/lib/components/mobile-nav/mobile-nav.svelteindex 03cee2c61..29411e964 100644--- a/frontend/src/lib/components/mobile-nav/mobile-nav.svelte+++ b/frontend/src/lib/components/mobile-nav/mobile-nav.svelte@@ -16,7 +16,7 @@let {user = null,versionInformation,- swarmEnabled = false,+ swarmEnabled,permissionsManifest = null,class: className = ''}: {@@ -29,8 +29,8 @@const navigation = getMobileNavigation();const navigationSettings = $derived(navigation.settings);- const swarmItems = $derived(getSwarmNavigationItems(swarmEnabled));const currentEnvId = $derived(environmentStore.selected?.id || '0');+ const swarmItems = $derived(getSwarmNavigationItems(swarmEnabled, currentEnvId));const pinnedItems = $derived.by(() => {if (!navigationSettings?.pinnedItems) return [];diff --git a/frontend/src/lib/components/sidebar/sidebar.svelte b/frontend/src/lib/components/sidebar/sidebar.svelteindex 4ad4c467f..6991e415f 100644--- a/frontend/src/lib/components/sidebar/sidebar.svelte+++ b/frontend/src/lib/components/sidebar/sidebar.svelte@@ -33,7 +33,7 @@variant = 'floating',user,versionInformation,- swarmEnabled = false,+ swarmEnabled,permissionsManifest = null,...restProps}: ComponentProps<typeof Sidebar.Root> & {@@ -57,7 +57,7 @@const currentEnvId = $derived(environmentStore.selected?.id || '0');const managementItemsRaw = $derived(getManagementItems(currentEnvId));- const swarmItemsRaw = $derived(getSwarmNavigationItems(swarmEnabled));+ const swarmItemsRaw = $derived(getSwarmNavigationItems(swarmEnabled, currentEnvId));const managementItems = $derived(filterByPermissions(managementItemsRaw, effectiveUser ?? null, currentEnvId, permissionsManifest)diff --git a/frontend/src/lib/config/features.ts b/frontend/src/lib/config/features.tsindex d89a961d0..c72887c77 100644--- a/frontend/src/lib/config/features.ts+++ b/frontend/src/lib/config/features.ts@@ -5,5 +5,10 @@ export const featureDefinitions = [id: 'vulnerabilityManagement',settingKey: 'featureVulnerabilityManagementEnabled',defaultEnabled: true+ },+ {+ id: 'swarm',+ settingKey: 'featureSwarmEnabled',+ defaultEnabled: false}] as const satisfies readonly FeatureDefinition[];diff --git a/frontend/src/lib/config/navigation-config.ts b/frontend/src/lib/config/navigation-config.tsindex b90244bd3..bfad8284a 100644--- a/frontend/src/lib/config/navigation-config.ts+++ b/frontend/src/lib/config/navigation-config.ts@@ -345,10 +345,13 @@ const defaultMobilePinnedItems: NavigationItem[] = [...navigationItems.managemen(item) => ['/dashboard', '/projects', '/containers', '/images'].includes(item.url));-export function getSwarmNavigationItems(swarmEnabled: boolean): NavigationItem[] {+export function getSwarmNavigationItems(swarmEnabled: boolean | undefined, currentEnvId: string): NavigationItem[] {if (swarmEnabled) {return navigationItems.swarmItems;}+ if (swarmEnabled === false && !featureStore.isSwarmEnabled(currentEnvId, false)) {+ return [];+ }return navigationItems.swarmItems.filter((item) => item.url === '/swarm/cluster');}@@ -387,7 +390,7 @@ export function getAvailableMobileNavItems(options?: {flatItems.push(...navigationItems.resourceItems);}- const swarmItems = getSwarmNavigationItems(!!options?.swarmEnabled);+ const swarmItems = getSwarmNavigationItems(options?.swarmEnabled, options?.currentEnvId ?? '0');if (swarmItems.length > 0) {flatItems.push(...swarmItems);}diff --git a/frontend/src/lib/query/query-keys.ts b/frontend/src/lib/query/query-keys.tsindex f3c2aa2de..916ca232f 100644--- a/frontend/src/lib/query/query-keys.ts+++ b/frontend/src/lib/query/query-keys.ts@@ -16,6 +16,7 @@ function stableSerialize(value: unknown): string {export const queryKeys = {swarm: {info: (environmentId: string) => ['swarm', environmentId, 'info'] as const,+ status: (environmentId: string) => ['swarm', environmentId, 'status'] as const,joinTokens: (environmentId: string) => ['swarm', environmentId, 'join-tokens'] as const,unlockKey: (environmentId: string) => ['swarm', environmentId, 'unlock-key'] as const,joinCandidates: (environmentId: string | null, user: Pick<User, 'id' | 'permissionsByEnv'> | null) =>diff --git a/frontend/src/lib/services/swarm-service.ts b/frontend/src/lib/services/swarm-service.tsindex d58cb37c3..29f07c18b 100644--- a/frontend/src/lib/services/swarm-service.ts+++ b/frontend/src/lib/services/swarm-service.ts@@ -216,8 +216,8 @@ class SwarmService extends BaseAPIService {return this.handleResponse(this.api.get(`/environments/${envId}/swarm/info`));}- async getSwarmStatus(): Promise<SwarmRuntimeStatus> {- const envId = await environmentStore.getCurrentEnvironmentId();+ async getSwarmStatus(environmentId?: string): Promise<SwarmRuntimeStatus> {+ const envId = environmentId ?? (await environmentStore.getCurrentEnvironmentId());return this.handleResponse(this.api.get(`/environments/${envId}/swarm/status`));}diff --git a/frontend/src/lib/stores/client-stream.svelte.ts b/frontend/src/lib/stores/client-stream.svelte.tsindex 70f555f2d..39114b217 100644--- a/frontend/src/lib/stores/client-stream.svelte.ts+++ b/frontend/src/lib/stores/client-stream.svelte.ts@@ -30,7 +30,8 @@ type ChannelSubscriber = {* nothing alike: environments is a single local query, while dashboard proxies* a request to every remote agent on every tick. Subscribing or unsubscribing* changes the URL, so the connection is reopened — the same mechanism the- * dashboard already used when its debug flag changed.+ * dashboard already used when its debug flag changed. Changes within one task+ * are coalesced into a single reopen.*/function createClientStreamInternal() {const subscribers = new Set<ChannelSubscriber>();@@ -61,25 +62,32 @@ function createClientStreamInternal() {}});- function reopen() {- if (!transport.isStarted) {- return;- }- if (activeChannels().length === 0) {- // Nothing left to carry; drop the connection rather than hold an- // idle one open with no channels.- transport.stop();- return;- }- transport.restart();- }+ let syncPending = false;- function ensureStarted() {- if (transport.isStarted || activeChannels().length === 0) {+ // Deferred to the next macrotask so back-to-back subscribes (the layout+ // starts several stores at once) open the connection once, not per channel.+ function scheduleSync() {+ if (syncPending) {return;}- transport.markStarted();- transport.connect(transport.nextGeneration());+ syncPending = true;+ setTimeout(() => {+ syncPending = false;+ if (activeChannels().length === 0) {+ // Nothing left to carry; drop the connection rather than hold an+ // idle one open with no channels.+ if (transport.isStarted) {+ transport.stop();+ }+ return;+ }+ if (!transport.isStarted) {+ transport.markStarted();+ transport.connect(transport.nextGeneration());+ return;+ }+ transport.restart();+ }, 0);}return {@@ -102,16 +110,14 @@ function createClientStreamInternal() {const hadChannel = activeChannels().includes(channel);subscribers.add(subscriber);- if (!transport.isStarted) {- ensureStarted();- } else if (!hadChannel) {- reopen();+ if (!transport.isStarted || !hadChannel) {+ scheduleSync();}return () => {subscribers.delete(subscriber);if (!activeChannels().includes(channel)) {- reopen();+ scheduleSync();}};},@@ -119,8 +125,8 @@ function createClientStreamInternal() {setParams(next: Record<string, string>) {const changed = JSON.stringify(next) !== JSON.stringify(params);params = next;- if (changed) {- reopen();+ if (changed && transport.isStarted) {+ scheduleSync();}},/** Reopens the connection, clearing a give-up state so it retries. */diff --git a/frontend/src/lib/stores/environment-stream.svelte.ts b/frontend/src/lib/stores/environment-stream.svelte.tsindex 61e806af7..5956c1140 100644--- a/frontend/src/lib/stores/environment-stream.svelte.ts+++ b/frontend/src/lib/stores/environment-stream.svelte.ts@@ -135,11 +135,12 @@ export function createEnvironmentStreamStore<TState extends StreamEnvStateBase,}async function refresh(generation = lifecycleGeneration) {- reconcileEnvironments();+ // The Promise.all below fetches every environment, including new ones.+ reconcileEnvironments(false);await Promise.all(Object.keys(_environmentStates).map((environmentId) => config.fetchSnapshot(environmentId, generation)));}- function reconcileEnvironments() {+ function reconcileEnvironments(fetchNew = true) {if (!browser || !started) {return;}@@ -161,10 +162,10 @@ export function createEnvironmentStreamStore<TState extends StreamEnvStateBase,}}- for (const environment of environments) reconcileEnvironment(environment);+ for (const environment of environments) reconcileEnvironment(environment, fetchNew);}- function reconcileEnvironment(environment: Pick<Environment, 'id' | 'name'>) {+ function reconcileEnvironment(environment: Pick<Environment, 'id' | 'name'>, fetchNew: boolean) {const environmentId = environment.id || LOCAL_DOCKER_ENVIRONMENT_ID;const existing = environmentState(environmentId);if (!existing) {@@ -175,7 +176,7 @@ export function createEnvironmentStreamStore<TState extends StreamEnvStateBase,// An already-open aggregated stream only picks new environments// up on its server-side reconcile tick; fetch once so the first// snapshot doesn't take up to that interval to appear.- if (clientStream.hasActiveStream) {+ if (fetchNew && clientStream.hasActiveStream) {void config.fetchSnapshot(environmentId, lifecycleGeneration);}return;@@ -231,11 +232,13 @@ export function createEnvironmentStreamStore<TState extends StreamEnvStateBase,return;}config.onSelectedEnvironment?.(environmentStore.selected);- reconcileEnvironments();- unsubscribeChannel = clientStream.subscribe(config.channel, channelHandlers);+ // refresh reconciles synchronously, so states exist before the channel subscribes.if (config.refreshOnStart) {void refresh(generation);+ } else {+ reconcileEnvironments();}+ unsubscribeChannel = clientStream.subscribe(config.channel, channelHandlers);unsubscribeEnvironment = environmentStore.subscribeSelected((environment) => {config.onSelectedEnvironment?.(environment);reconcileEnvironments();diff --git a/frontend/src/lib/stores/features.store.svelte.ts b/frontend/src/lib/stores/features.store.svelte.tsindex cdb526464..0e83e9bc0 100644--- a/frontend/src/lib/stores/features.store.svelte.ts+++ b/frontend/src/lib/stores/features.store.svelte.ts@@ -1,7 +1,7 @@import type { QueryClient } from '@tanstack/svelte-query';import { settingsService } from '#lib/services/settings-service.js';import type { EnvironmentFeatures, FeatureID } from '#lib/types/features.js';-import { isFeatureEnabled, isVulnerabilityQuery, resolveFeatures } from '#lib/utils/features.js';+import { isFeatureEnabled, isSwarmFeatureEnabled, isVulnerabilityQuery, resolveFeatures } from '#lib/utils/features.js';let environments = $state.raw<Record<string, EnvironmentFeatures>>({});const requests = new Map<string, Promise<void>>();@@ -68,6 +68,8 @@ export const featureStore = {refreshKnown: () => Promise.all(Object.keys(environments).map((id) => load(id, true))),status: (environmentId: string) => environments[environmentId]?.status ?? 'loading',isEnabled: (id: FeatureID, environmentId: string) => isFeatureEnabled(environments[environmentId], id),+ isSwarmEnabled: (environmentId: string, swarmActive: boolean) =>+ isSwarmFeatureEnabled(environments[environmentId], swarmActive),isSupported: (id: FeatureID, environmentId: string) =>environments[environmentId]?.status === 'ready' && environments[environmentId]?.features[id]?.supported === true,connect(client: QueryClient) {diff --git a/frontend/src/lib/types/features.ts b/frontend/src/lib/types/features.tsindex b025b68e0..12ce03b45 100644--- a/frontend/src/lib/types/features.ts+++ b/frontend/src/lib/types/features.ts@@ -1,8 +1,8 @@-export type FeatureID = 'vulnerabilityManagement';+export type FeatureID = 'vulnerabilityManagement' | 'swarm';export type FeatureDefinition = {id: FeatureID;- settingKey: 'featureVulnerabilityManagementEnabled';+ settingKey: 'featureVulnerabilityManagementEnabled' | 'featureSwarmEnabled';defaultEnabled: boolean;};diff --git a/frontend/src/lib/types/settings.ts b/frontend/src/lib/types/settings.tsindex b6b52794f..edc2086ff 100644--- a/frontend/src/lib/types/settings.ts+++ b/frontend/src/lib/types/settings.ts@@ -54,6 +54,7 @@ export type Settings = {pruneBuildCacheUntil?: string;vulnerabilityScanEnabled?: boolean;featureVulnerabilityManagementEnabled?: boolean;+ featureSwarmEnabled?: boolean;vulnerabilityScanInterval?: number;autoHealEnabled?: boolean;autoHealExcludedContainers?: string;diff --git a/frontend/src/lib/utils/features.ts b/frontend/src/lib/utils/features.tsindex 4f44852c1..8ca47861e 100644--- a/frontend/src/lib/utils/features.ts+++ b/frontend/src/lib/utils/features.ts@@ -18,6 +18,14 @@ export function isFeatureEnabled(state: EnvironmentFeatures | undefined, id: Feareturn state?.status === 'ready' && state.features[id]?.enabled === true;}+// An active swarm cluster keeps Swarm on, and agents that predate the toggle keep showing it.+export function isSwarmFeatureEnabled(state: EnvironmentFeatures | undefined, swarmActive: boolean): boolean {+ if (swarmActive) return true;+ if (state?.status !== 'ready') return false;+ const swarm = state.features.swarm;+ return swarm?.supported !== true || swarm.enabled;+}+export function isVulnerabilityQuery(queryKey: readonly unknown[], environmentId: string): boolean {return queryKey[0] === 'vulnerabilities' && queryKey[2] === environmentId;}diff --git a/frontend/src/routes/(app)/+layout.svelte b/frontend/src/routes/(app)/+layout.svelteindex d8b3badf0..b6222b0d7 100644--- a/frontend/src/routes/(app)/+layout.svelte+++ b/frontend/src/routes/(app)/+layout.svelte@@ -33,7 +33,7 @@const user = $derived(data.user);const permissionsManifest = $derived(data.permissionsManifest);const permissionsManifestLoadFailed = $derived(data.permissionsManifestLoadFailed);- const swarmEnabled = $derived(data.swarmEnabled === true);+ const swarmEnabled = $derived(data.swarmEnabled);const canReadActivities = $derived(userHasPermissionInAnyEnvironment(user, 'activities:read'));const isMobile = new IsMobile();diff --git a/frontend/src/routes/(app)/environments/[id]/+page.svelte b/frontend/src/routes/(app)/environments/[id]/+page.svelteindex 9a667f0ba..336a018b2 100644--- a/frontend/src/routes/(app)/environments/[id]/+page.svelte+++ b/frontend/src/routes/(app)/environments/[id]/+page.svelte@@ -4,6 +4,8 @@import LabeledSwitch from '#lib/components/form/labeled-switch.svelte';import TextInputWithLabel from '#lib/components/form/text-input-with-label.svelte';import { featureStore } from '#lib/stores/features.store.svelte.js';+ import { featureDefinitions } from '#lib/config/features.js';+ import type { FeatureID } from '#lib/types/features.js';import { tryCatch } from '#lib/utils/try-catch.js';import { onMount } from 'svelte';@@ -65,6 +67,7 @@let { data } = $props();let { settings, versionInformation } = $derived(data);+ const swarmActive = $derived(data.swarmActive === true);let lastEnvironment: Environment | undefined;let loadedEnvironment = $derived((lastEnvironment = data.environment ?? lastEnvironment));let environment = $derived(loadedEnvironment);@@ -326,6 +329,7 @@pruneBuildCacheMode: settings?.pruneBuildCacheMode ?? 'none',pruneBuildCacheUntil: settings?.pruneBuildCacheUntil ?? '',featureVulnerabilityManagementEnabled: settings?.featureVulnerabilityManagementEnabled ?? true,+ featureSwarmEnabled: settings?.featureSwarmEnabled ?? false,vulnerabilityScanEnabled: settings?.vulnerabilityScanEnabled ?? false,toolsImageRegistry: settings?.toolsImageRegistry ?? 'ghcr.io',updateCheckRegistry: settings?.updateCheckRegistry ?? 'auto',@@ -363,11 +367,13 @@const environmentId = environment.id;const submittedInputs = formInputs;const apiUrlChanged = formData.apiUrl !== environment.apiUrl;- const featureChanged =- formData.featureVulnerabilityManagementEnabled !== currentSettings.featureVulnerabilityManagementEnabled;+ const changedFeatures = featureDefinitions.filter(+ (feature) => formData[feature.settingKey] !== currentSettings[feature.settingKey]+ );if (- featureChanged &&- (!hasPermission('settings:write', environmentId) || !featureStore.isSupported('vulnerabilityManagement', environmentId))+ changedFeatures.length > 0 &&+ (!hasPermission('settings:write', environmentId) ||+ changedFeatures.some((feature) => !featureStore.isSupported(feature.id, environmentId)))) {throw new Error(m.features_unavailable());}@@ -388,10 +394,15 @@}const parsedCurrentSettings = formSchema.safeParse(currentSettings);const savedFormValues = parsedCurrentSettings.success ? parsedCurrentSettings.data : currentSettings;+ const separatelySavedKeys: string[] = [+ 'name',+ 'enabled',+ 'apiUrl',+ 'accessToken',+ ...featureDefinitions.map((feature) => feature.settingKey)+ ];const otherSettingsChanged = (Object.keys(formData) as (keyof EnvironmentFormValues)[]).some(- (key) =>- !['name', 'enabled', 'apiUrl', 'accessToken', 'featureVulnerabilityManagementEnabled'].includes(key) &&- formData[key] !== savedFormValues[key]+ (key) => !separatelySavedKeys.includes(key) && formData[key] !== savedFormValues[key]);let updates: Partial<Settings> = {};@@ -456,14 +467,15 @@autoHealRestartWindow: formData.autoHealRestartWindow};}- if (featureChanged) updates.featureVulnerabilityManagementEnabled = formData.featureVulnerabilityManagementEnabled;+ for (const feature of changedFeatures) updates[feature.settingKey] = formData[feature.settingKey];if (Object.keys(updates).length > 0) await settingsService.updateSettingsForEnvironment(environmentId, updates);- if (featureChanged) {+ if (changedFeatures.length > 0) {await featureStore.refresh(environmentId);if (featureStore.status(environmentId) !== 'ready') throw new Error(m.features_unavailable());- if (featureStore.isEnabled('vulnerabilityManagement', environmentId) !== formData.featureVulnerabilityManagementEnabled) {- throw new Error(m.features_environment_override());- }+ const overridden = changedFeatures.find(+ (feature) => featureStore.isEnabled(feature.id, environmentId) !== formData[feature.settingKey]+ );+ if (overridden) throw new Error(featureOverrideMessage(overridden.id));}await refreshEnvironment();@@ -480,6 +492,25 @@}}+ function featureOverrideMessage(id: FeatureID): string {+ switch (id) {+ case 'swarm':+ return m.features_swarm_environment_override();+ case 'vulnerabilityManagement':+ return m.features_environment_override();+ }+ }++ function featureSwitchDisabled(id: FeatureID): boolean {+ return (+ !isCurrentlyOnline ||+ !settings ||+ settings.uiConfigDisabled ||+ !hasPermission('settings:write', environment.id) ||+ !featureStore.isSupported(id, environment.id)+ );+ }+function clearAccessToken(): void {formInputs.accessToken.value = '';}@@ -862,19 +893,30 @@<section id="features"><SectionCard title={m.features_title()} icon={SettingsIcon} variant="transparent">{#if featureStore.status(environment.id) === 'ready'}- <LabeledSwitch- id="vulnerability-management"- bind:checked={formInputs.featureVulnerabilityManagementEnabled.value}- label={m.features_vulnerability_management()}- description={m.features_vulnerability_description()}- error={formInputs.featureVulnerabilityManagementEnabled.error}- disabled={!isCurrentlyOnline ||- !settings ||- settings.uiConfigDisabled ||- !hasPermission('settings:write', environment.id) ||- !featureStore.isSupported('vulnerabilityManagement', environment.id)}- />- {#if !featureStore.isSupported('vulnerabilityManagement', environment.id)}+ <div class="space-y-6">+ <LabeledSwitch+ id="vulnerability-management"+ bind:checked={formInputs.featureVulnerabilityManagementEnabled.value}+ label={m.features_vulnerability_management()}+ description={m.features_vulnerability_description()}+ error={formInputs.featureVulnerabilityManagementEnabled.error}+ disabled={featureSwitchDisabled('vulnerabilityManagement')}+ />+ {#if swarmActive}+ <!-- An active cluster keeps Swarm on, so the stored toggle is shown locked. -->+ <LabeledSwitch id="swarm" checked={true} label={m.swarm()} description={m.features_swarm_locked()} disabled />+ {:else}+ <LabeledSwitch+ id="swarm"+ bind:checked={formInputs.featureSwarmEnabled.value}+ label={m.swarm()}+ description={m.features_swarm_description()}+ error={formInputs.featureSwarmEnabled.error}+ disabled={featureSwitchDisabled('swarm')}+ />+ {/if}+ </div>+ {#if featureDefinitions.some((feature) => !featureStore.isSupported(feature.id, environment.id))}<p role="status" class="mt-4 text-sm text-muted-foreground">{m.features_unsupported()}</p>{/if}{:else}diff --git a/frontend/src/routes/(app)/environments/[id]/+page.ts b/frontend/src/routes/(app)/environments/[id]/+page.tsindex d795eeeb8..22a44041e 100644--- a/frontend/src/routes/(app)/environments/[id]/+page.ts+++ b/frontend/src/routes/(app)/environments/[id]/+page.ts@@ -2,6 +2,7 @@ import { featureStore } from '#lib/stores/features.store.svelte.js';import { tryCatch } from '#lib/utils/try-catch.js';import { environmentManagementService } from '#lib/services/env-mgmt-service.js';import { settingsService } from '#lib/services/settings-service.js';+import { swarmService } from '#lib/services/swarm-service.js';import { queryKeys } from '#lib/query/query-keys.js';import { userHasPermission } from '#lib/utils/auth.js';import { isEnvironmentOnline } from '#lib/utils/docker.js';@@ -18,9 +19,18 @@ export const load: PageLoad = async ({ params, parent }) => {});if (!environment.enabled || !isEnvironmentOnline(environment)) {await featureStore.markUnavailable(params.id);- return { environment, settings: null };+ return { environment, settings: null, swarmActive: false };}await featureStore.load(params.id);+ // An unknown status only leaves the Swarm toggle unlocked; nothing else depends on it.+ const swarmActiveRequest = userHasPermission(user, 'swarm:read', params.id)+ ? tryCatch(+ queryClient.query({+ queryKey: queryKeys.swarm.status(params.id),+ queryFn: () => swarmService.getSwarmStatus(params.id)+ })+ ).then((result) => result.data?.enabled === true)+ : Promise.resolve(false);let settings = null;const operationResult = await tryCatch(@@ -40,7 +50,8 @@ export const load: PageLoad = async ({ params, parent }) => {return {environment,- settings+ settings,+ swarmActive: await swarmActiveRequest};})());diff --git a/frontend/src/routes/(app)/environments/[id]/components/environment-form-schema.ts b/frontend/src/routes/(app)/environments/[id]/components/environment-form-schema.tsindex 4c8b58798..9f79aaa98 100644--- a/frontend/src/routes/(app)/environments/[id]/components/environment-form-schema.ts+++ b/frontend/src/routes/(app)/environments/[id]/components/environment-form-schema.ts@@ -42,6 +42,7 @@ export const environmentFormSchema = environmentUpdateSchemapruneBuildCacheMode: z.enum(['none', 'unused', 'all', 'olderThan']),pruneBuildCacheUntil: z.string(),featureVulnerabilityManagementEnabled: z.boolean(),+ featureSwarmEnabled: z.boolean(),vulnerabilityScanEnabled: z.boolean(),toolsImageRegistry: z.enum(['ghcr.io', 'docker.io']),updateCheckRegistry: z.enum(['auto', 'ghcr.io', 'docker.io', 'public.ecr.aws']),diff --git a/frontend/src/routes/(app)/images/+page.svelte b/frontend/src/routes/(app)/images/+page.svelteindex 39c47e52b..f11d6acde 100644--- a/frontend/src/routes/(app)/images/+page.svelte+++ b/frontend/src/routes/(app)/images/+page.svelte@@ -159,10 +159,10 @@imageUsageCountsQuery.data?.envId === envId ? imageUsageCountsQuery.data.value : imageUsageFallback);- // Intentionally a manual flag, not $derived(query.isFetching): these queries use- // aggressive background refetching (staleTime: 0 + refetchOnMount/WindowFocus:- // 'always'), so deriving from isFetching leaves the manual refresh button spinning- // constantly. Mirrors the containers page — reflects only user-initiated refreshes.+ // Intentionally a manual flag, not $derived(query.isFetching): these queries also+ // refetch in the background (on mount once stale, and always on window focus), so+ // deriving from isFetching would spin the manual refresh button on every background+ // fetch. Mirrors the containers page — reflects only user-initiated refreshes.const isUploading = $derived(uploadImagesMutation.isPending);const isPruning = $derived(pruneImagesMutation.isPending);const isChecking = $derived(checkUpdatesMutation.isPending);diff --git a/frontend/src/routes/+layout.ts b/frontend/src/routes/+layout.tsindex 21c42483e..97095f463 100644--- a/frontend/src/routes/+layout.ts+++ b/frontend/src/routes/+layout.ts@@ -28,9 +28,11 @@ const queryClient = new QueryClient({defaultOptions: {queries: {enabled: browser,- staleTime: 0,+ // Covers the gap between a page loader's fetch and the component's+ // mount so the same query isn't fetched twice in one navigation.+ staleTime: 2_000,gcTime: 60 * 1000,- refetchOnMount: 'always',+ refetchOnMount: true,refetchOnWindowFocus: 'always',refetchOnReconnect: 'always'}@@ -92,7 +94,7 @@ export const load: LayoutLoad = async ({ url }) => {}let settings = null;- let swarmEnabled = false;+ let swarmEnabled: boolean | undefined;let permissionsManifest: PermissionsManifest | null = null;let permissionsManifestLoadFailed = false;if (user) {@@ -100,7 +102,7 @@ export const load: LayoutLoad = async ({ url }) => {const environmentRequestOptions: SearchPaginationSortRequest = {pagination: {page: 1,- limit: 1000+ limit: -1}};@@ -138,7 +140,7 @@ export const load: LayoutLoad = async ({ url }) => {loadedSettings ??(await tryCatch(settingsService.getPublicSettings()).then((result) => (result.error ? null : result.data)));await featuresRequest;- swarmEnabled = loadedSwarmStatus?.enabled === true;+ swarmEnabled = loadedSwarmStatus?.enabled;permissionsManifest = loadedPermissionsManifest;permissionsManifestLoadFailed = loadedPermissionsManifest === null;} else {diff --git a/types/features/features.go b/types/features/features.goindex ebb295179..2da30d750 100644--- a/types/features/features.go+++ b/types/features/features.go@@ -10,6 +10,13 @@ const (// VulnerabilityManagementSettingKey is the persisted toggle for vulnerability management.VulnerabilityManagementSettingKey = "featureVulnerabilityManagementEnabled"++ // Swarm identifies the Docker Swarm pages. An active swarm cluster keeps it enabled+ // regardless of the persisted toggle.+ Swarm ID = "swarm"++ // SwarmSettingKey is the persisted toggle for Docker Swarm.+ SwarmSettingKey = "featureSwarmEnabled")// Definition connects a runtime feature to its persisted setting and default.@@ -21,6 +28,7 @@ type Definition struct {var registryInternal = [...]Definition{{ID: VulnerabilityManagement, SettingKey: VulnerabilityManagementSettingKey, DefaultEnabled: true},+ {ID: Swarm, SettingKey: SwarmSettingKey, DefaultEnabled: false},}// All returns the supported runtime features.diff --git a/types/features/features_test.go b/types/features/features_test.goindex a3ec690cf..6f12862ac 100644--- a/types/features/features_test.go+++ b/types/features/features_test.go@@ -16,6 +16,7 @@ func TestRegistry(t *testing.T) {}{{name: "known feature", id: features.VulnerabilityManagement, wantFound: true, want: features.Definition{ID: features.VulnerabilityManagement, SettingKey: "featureVulnerabilityManagementEnabled", DefaultEnabled: true}},{name: "returned definitions are independent", id: features.VulnerabilityManagement, mutateCopy: true, wantFound: true, want: features.Definition{ID: features.VulnerabilityManagement, SettingKey: "featureVulnerabilityManagementEnabled", DefaultEnabled: true}},+ {name: "swarm defaults off", id: features.Swarm, wantFound: true, want: features.Definition{ID: features.Swarm, SettingKey: "featureSwarmEnabled", DefaultEnabled: false}},{name: "unknown feature", id: features.ID("unknown")},}for _, tt := range tests {diff --git a/types/settings/settings.go b/types/settings/settings.goindex b937d8e5a..b14fe376e 100644--- a/types/settings/settings.go+++ b/types/settings/settings.go@@ -194,6 +194,9 @@ type Update struct {// FeatureVulnerabilityManagementEnabled controls vulnerability management for this environment.FeatureVulnerabilityManagementEnabled *string `json:"featureVulnerabilityManagementEnabled,omitzero" enum:"true,false"`+ // FeatureSwarmEnabled controls Docker Swarm for this environment. An active swarm cluster keeps it enabled.+ FeatureSwarmEnabled *string `json:"featureSwarmEnabled,omitzero" enum:"true,false"`+// VulnerabilityScanEnabled indicates if scheduled vulnerability scanning is enabled.//// Required: false