Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: migrate containers environments and networks to composite structure
Kyle Mendelld2b4166dfbb6
diff --git a/backend/internal/container/children/stats/service.go b/backend/internal/container/children/stats/service.goindex f9622673c..1c349cb02 100644--- a/backend/internal/container/children/stats/service.go+++ b/backend/internal/container/children/stats/service.go@@ -59,14 +59,22 @@ type cachedStats struct {fetchedAt time.Time}+type resourceSample struct {+ stats container.StatsResponse+ inspect *container.InspectResponse+}++type resourceInstruments struct {+ cpuTime metric.Float64ObservableCounter+ cpuUtilization, uptime metric.Float64ObservableGauge+ memoryUsage, memoryLimit metric.Int64ObservableGauge+ pids metric.Int64ObservableGauge+ networkIO, diskIO metric.Int64ObservableCounter+ restarts metric.Int64ObservableCounter+}+// New builds a stats service. Non-positive timeouts fall back to defaults.func New(getClient func(context.Context) (*client.Client, error), dockerHost func() string, cacheTTL, sampleTimeout, batchTimeout time.Duration) *Service {- if sampleTimeout <= 0 {- sampleTimeout = ContainerResourceCollectTimeout- }- if batchTimeout <= 0 {- batchTimeout = timeouts.DefaultDockerAPI- }return &Service{getClient: getClient,dockerHost: dockerHost,@@ -79,8 +87,8 @@ func New(getClient func(context.Context) (*client.Client, error), dockerHost funWithTTL(time.Minute).WithJanitor().Build(),- sampleTimeout: sampleTimeout,- batchTimeout: batchTimeout,+ sampleTimeout: kit.Ternary(sampleTimeout > 0, sampleTimeout, ContainerResourceCollectTimeout),+ batchTimeout: kit.Ternary(batchTimeout > 0, batchTimeout, timeouts.DefaultDockerAPI),}}@@ -116,16 +124,12 @@ func (s *Service) Stream(ctx context.Context, containerID string, statsChan chanreturn fmt.Errorf("failed to decode stats: %w", unmarshalDecodeErr)}- recordedAt := statsData.Read- if recordedAt.IsZero() {- recordedAt = time.Now()- }-+ currentSample := stats.BuildSample(statsData)payload := stats.StatsStreamPayload{StatsResponse: statsData,- CurrentHistorySample: stats.BuildSample(statsData),+ CurrentHistorySample: currentSample,+ StatsHistory: s.history.Record(containerID, currentSample, !historySent, kit.Ternary(statsData.Read.IsZero(), time.Now(), statsData.Read)),}- payload.StatsHistory = s.history.Record(containerID, payload.CurrentHistorySample, !historySent, recordedAt)historySent = trueselect {@@ -221,7 +225,7 @@ func (s *Service) fetch(ctx context.Context, containerID string, maxAge time.Durreturn nil, fmt.Errorf("failed to decode container stats: %w", decodeErr)}if statsData.Read.IsZero() {- statsData.Read = time.Now()+ statsData.Read = time.Now() // keep: StatsResponse is filled by json.UnmarshalDecode and has ~15 nested fields}s.cache.Set(key, cachedStats{stats: statsData, fetchedAt: time.Now()})return statsData, nil@@ -244,46 +248,41 @@ func (s *Service) fetch(ctx context.Context, containerID string, maxAge time.Dur}}-type resourceSample struct {- stats container.StatsResponse- inspect *container.InspectResponse-}--type resourceInstruments struct {- cpuTime metric.Float64ObservableCounter- cpuUtilization, uptime metric.Float64ObservableGauge- memoryUsage, memoryLimit metric.Int64ObservableGauge- pids metric.Int64ObservableGauge- networkIO, diskIO metric.Int64ObservableCounter- restarts metric.Int64ObservableCounter-}-// ObserveResources reports arcane.container.* metrics for running containers from list, sharing the// sample cache and flights with Collect so overlapping collections reuse Docker calls.func (s *Service) ObserveResources(meter metric.Meter, list func(context.Context) ([]container.Summary, error)) error {- var r resourceInstruments- var errs [9]error- r.cpuTime, errs[0] = meter.Float64ObservableCounter("arcane.container.cpu.time",+ cpuTime, cpuTimeErr := meter.Float64ObservableCounter("arcane.container.cpu.time",metric.WithDescription("Cumulative CPU time consumed by a running container"), metric.WithUnit("s"))- r.cpuUtilization, errs[1] = meter.Float64ObservableGauge("arcane.container.cpu.utilization",+ cpuUtilization, cpuUtilizationErr := meter.Float64ObservableGauge("arcane.container.cpu.utilization",metric.WithDescription("Container CPU usage as a fraction of total host CPU capacity"), metric.WithUnit("1"))- r.memoryUsage, errs[2] = meter.Int64ObservableGauge("arcane.container.memory.usage",+ memoryUsage, memoryUsageErr := meter.Int64ObservableGauge("arcane.container.memory.usage",metric.WithDescription("Container memory usage excluding inactive page cache"), metric.WithUnit("By"))- r.memoryLimit, errs[3] = meter.Int64ObservableGauge("arcane.container.memory.limit",+ memoryLimit, memoryLimitErr := meter.Int64ObservableGauge("arcane.container.memory.limit",metric.WithDescription("Container memory limit, or host memory when unlimited"), metric.WithUnit("By"))- r.pids, errs[4] = meter.Int64ObservableGauge("arcane.container.pids",+ pids, pidsErr := meter.Int64ObservableGauge("arcane.container.pids",metric.WithDescription("Processes and threads running in a container"), metric.WithUnit("{process}"))- r.networkIO, errs[5] = meter.Int64ObservableCounter("arcane.container.network.io",+ networkIO, networkIOErr := meter.Int64ObservableCounter("arcane.container.network.io",metric.WithDescription("Bytes received and transmitted across all container network interfaces"), metric.WithUnit("By"))- r.diskIO, errs[6] = meter.Int64ObservableCounter("arcane.container.disk.io",+ diskIO, diskIOErr := meter.Int64ObservableCounter("arcane.container.disk.io",metric.WithDescription("Bytes read from and written to block devices by a container"), metric.WithUnit("By"))- r.restarts, errs[7] = meter.Int64ObservableCounter("arcane.container.restarts",+ restarts, restartsErr := meter.Int64ObservableCounter("arcane.container.restarts",metric.WithDescription("Times Docker restarted the container under its restart policy"), metric.WithUnit("{restart}"))- r.uptime, errs[8] = meter.Float64ObservableGauge("arcane.container.uptime",+ uptime, uptimeErr := meter.Float64ObservableGauge("arcane.container.uptime",metric.WithDescription("Time since the container last started"), metric.WithUnit("s"))- if err := errors.Join(errs[:]...); err != nil {+ if err := errors.Join(cpuTimeErr, cpuUtilizationErr, memoryUsageErr, memoryLimitErr, pidsErr, networkIOErr, diskIOErr, restartsErr, uptimeErr); err != nil {return err}+ r := resourceInstruments{+ cpuTime: cpuTime,+ cpuUtilization: cpuUtilization,+ uptime: uptime,+ memoryUsage: memoryUsage,+ memoryLimit: memoryLimit,+ pids: pids,+ networkIO: networkIO,+ diskIO: diskIO,+ restarts: restarts,+ }_, err := meter.RegisterCallback(func(ctx context.Context, o metric.Observer) error {containers, err := list(ctx)@@ -302,16 +301,24 @@ func (s *Service) ObserveResources(meter metric.Meter, list func(context.Contextif fetchErr != nil {return resourceSample{}, fetchErr}- sample := resourceSample{stats: statsData}- if cached, ok, _ := s.inspectCache.Get(id); ok {- sample.inspect = &cached- } else if dockerClient, clientErr := s.getClient(ctx); clientErr == nil {- if inspect, inspectErr := dockerClient.ContainerInspect(ctx, id, client.ContainerInspectOptions{}); inspectErr == nil {+ return resourceSample{+ stats: statsData,+ inspect: func() *container.InspectResponse {+ if cached, ok, _ := s.inspectCache.Get(id); ok {+ return &cached+ }+ dockerClient, clientErr := s.getClient(ctx)+ if clientErr != nil {+ return nil+ }+ inspect, inspectErr := dockerClient.ContainerInspect(ctx, id, client.ContainerInspectOptions{})+ if inspectErr != nil {+ return nil+ }s.inspectCache.Set(id, inspect.Container)- sample.inspect = &inspect.Container- }- }- return sample, nil+ return &inspect.Container+ }(),+ }, nil})if err != nil {slog.DebugContext(ctx, "Container resource metrics are partial", "error", err)@@ -371,10 +378,8 @@ func (r *resourceInstruments) observe(o metric.Observer, c container.Summary, sareturn}o.ObserveInt64(r.restarts, int64(sample.inspect.RestartCount), labels)- if sample.inspect.State != nil {- if startedAt, err := time.Parse(time.RFC3339Nano, sample.inspect.State.StartedAt); err == nil {- o.ObserveFloat64(r.uptime, time.Since(startedAt).Seconds(), labels)- }+ if startedAt, err := time.Parse(time.RFC3339Nano, kit.FromPtr(sample.inspect.State).StartedAt); err == nil {+ o.ObserveFloat64(r.uptime, time.Since(startedAt).Seconds(), labels)}}@@ -409,23 +414,18 @@ func ContainerResourceSampleSort(sort string, descending bool) pagination.SortOpif valueA == valueB {return tieBreak(a, b)}- less := valueA < valueB- if descending {- less = !less- }- return kit.Ternary(less, -1, 1)+ return kit.Ternary((valueA < valueB) != descending, -1, 1)}}func CompareContainerNamesForSort(a, b containertypes.Summary) int {- nameA, nameB := "", ""- if len(a.Names) > 0 {- nameA = a.Names[0]- }- if len(b.Names) > 0 {- nameB = b.Names[0]+ firstName := func(names []string) string {+ if len(names) == 0 {+ return ""+ }+ return names[0]}- return strings.Compare(nameA, nameB)+ return strings.Compare(firstName(a.Names), firstName(b.Names))}// ContainerResourceSortPermissionDenied enforces the extra containers:readdiff --git a/backend/internal/container/children/stats/service_test.go b/backend/internal/container/children/stats/service_test.goindex bdfd3fefe..007183933 100644--- a/backend/internal/container/children/stats/service_test.go+++ b/backend/internal/container/children/stats/service_test.go@@ -34,7 +34,11 @@ func TestResourceSortPermissionCheck(t *testing.T) {func TestResourceSortComparatorKeepsUnavailableLast(t *testing.T) {asc := ContainerResourceSampleSort(container.SortMemoryUsage, false)desc := ContainerResourceSampleSort(container.SortMemoryUsage, true)- sampled := container.Summary{ID: "b", Names: []string{"b"}, ResourceSample: &container.ResourceSample{MemoryUsageBytes: 1}}+ sampled := container.Summary{+ ID: "b",+ Names: []string{"b"},+ ResourceSample: &container.ResourceSample{MemoryUsageBytes: 1},+ }unsampled := container.Summary{ID: "a", Names: []string{"a"}}require.Equal(t, 1, asc(unsampled, sampled))@@ -42,7 +46,11 @@ func TestResourceSortComparatorKeepsUnavailableLast(t *testing.T) {require.Equal(t, 1, desc(unsampled, sampled))require.Equal(t, -1, desc(sampled, unsampled))- zero := container.Summary{ID: "z", Names: []string{"z"}, ResourceSample: &container.ResourceSample{MemoryUsageBytes: 0}}+ zero := container.Summary{+ ID: "z",+ Names: []string{"z"},+ ResourceSample: &container.ResourceSample{MemoryUsageBytes: 0},+ }require.Equal(t, -1, asc(zero, unsampled), "zero is a valid value, not unavailable")require.Equal(t, -1, asc(zero, sampled))}diff --git a/backend/internal/container/handler.go b/backend/internal/container/handler.goindex dccb77436..6b46730df 100644--- a/backend/internal/container/handler.go+++ b/backend/internal/container/handler.go@@ -2,11 +2,9 @@ package containerimport ("context"- "errors""fmt""io""maps"- "net/netip""strings""github.com/containerd/errdefs"@@ -138,6 +136,16 @@ type GenerateComposeInput struct {Body container.GenerateComposeRequest}+type containerActionConfig struct {+ ActivityType activitytypes.Type+ Step string+ StartMessage string+ CompleteMessage string+ SuccessMessage string+ Action func(context.Context, string, usertypes.Actor) error+ Error func(error) error+}+func (h *ContainerHandler) ListContainers(ctx context.Context, input *ListContainersInput) (*ListContainersOutput, error) {params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)if input.Updates != "" {@@ -187,270 +195,144 @@ func (h *ContainerHandler) GetContainerStatusCounts(ctx context.Context, input *containers = FilterExcludedContainers(containers, input.IncludeInternal, input.IncludeHidden)- running, stopped := 0, 0+ running := 0for _, c := range containers {if c.State == "running" {running++- } else {- stopped++}}- total := len(containers)return &handlerutil.Out[container.StatusCounts]{Body: base.ApiResponse[container.StatusCounts]{Success: true,Data: container.StatusCounts{RunningContainers: running,- StoppedContainers: stopped,- TotalContainers: total,+ StoppedContainers: len(containers) - running,+ TotalContainers: len(containers),},},}, nil}-func parsePortSpec(spec string) (network.Port, error) {- if strings.Contains(spec, "/") {- return network.ParsePort(spec)- }-- return network.ParsePort(spec + "/tcp")-}--func buildCreateLabels(body container.Create) map[string]string {- labels := map[string]string{- "com.arcane.created": "true",- }- maps.Copy(labels, body.Labels)-- return labels-}--func buildContainerConfig(body container.Create) *dockercontainer.Config {- return &dockercontainer.Config{- Image: body.Image,- Cmd: kit.Ternary(len(body.Command) > 0, body.Command, body.Cmd),- Entrypoint: body.Entrypoint,- WorkingDir: body.WorkingDir,- User: body.User,- Env: kit.Ternary(len(body.Environment) > 0, body.Environment, body.Env),- ExposedPorts: network.PortSet{},- Labels: buildCreateLabels(body),- Healthcheck: healthConfigFromCreateInternal(body.Healthcheck),- Hostname: body.Hostname,- Domainname: body.Domainname,- AttachStdout: body.AttachStdout,- AttachStderr: body.AttachStderr,- AttachStdin: body.AttachStdin,- Tty: body.Tty,- OpenStdin: body.OpenStdin,- StdinOnce: body.StdinOnce,- NetworkDisabled: body.NetworkDisabled,+func (h *ContainerHandler) CreateContainer(ctx context.Context, input *CreateContainerInput) (*handlerutil.Out[container.Created], error) {+ user, err := handlerutil.RequireUser(ctx)+ if err != nil {+ return nil, err}-}-func applyLegacyPortBindings(body container.Create, config *dockercontainer.Config, portBindings network.PortMap) error {- for containerPort, hostPort := range body.Ports {- port, err := network.ParsePort(containerPort + "/tcp")- if err != nil {- return err+ exposedPorts := network.PortSet{}+ portBindings := network.PortMap{}+ for containerPort, hostPort := range input.Body.Ports {+ port, parsePortErr := network.ParsePort(containerPort + "/tcp")+ if parsePortErr != nil {+ return nil, huma.Error400BadRequest("Invalid port format: " + parsePortErr.Error())}- config.ExposedPorts[port] = struct{}{}+ exposedPorts[port] = struct{}{}portBindings[port] = []network.PortBinding{{HostPort: hostPort}}}-- return nil-}--func applyExposedPorts(exposedPorts map[string]struct{}, config *dockercontainer.Config) error {- for portSpec := range exposedPorts {- port, err := parsePortSpec(portSpec)- if err != nil {- return err+ for portSpec := range input.Body.ExposedPorts {+ port, parsePortErr := network.ParsePort(kit.Ternary(strings.Contains(portSpec, "/"), portSpec, portSpec+"/tcp"))+ if parsePortErr != nil {+ return nil, huma.Error400BadRequest("Invalid port format: " + parsePortErr.Error())}- config.ExposedPorts[port] = struct{}{}+ exposedPorts[port] = struct{}{}}- return nil-}--func buildHostConfigBase(body container.Create, portBindings network.PortMap) *dockercontainer.HostConfig {- return &dockercontainer.HostConfig{- Binds: body.Volumes,- PortBindings: portBindings,- Privileged: body.Privileged,- AutoRemove: body.AutoRemove,- RestartPolicy: dockercontainer.RestartPolicy{Name: dockercontainer.RestartPolicyMode(body.RestartPolicy)},+ hostConfig := kit.FromPtr(input.Body.HostConfig)+ hostBindings, err := portMapFromCreate(hostConfig.PortBindings)+ if err != nil {+ return nil, huma.Error400BadRequest("Invalid port format: " + err.Error())}-}--func applyHostConfigPortBindings(config *dockercontainer.Config, portBindings network.PortMap, bindings map[string][]container.PortBindingCreate) error {- for portSpec, bindingList := range bindings {- port, err := parsePortSpec(portSpec)- if err != nil {- return err- }- config.ExposedPorts[port] = struct{}{}- for _, binding := range bindingList {- pb := network.PortBinding{HostPort: binding.HostPort}- if hostIP := strings.TrimSpace(binding.HostIP); hostIP != "" {- parsedIP, parseAddrErr := netip.ParseAddr(hostIP)- if parseAddrErr != nil {- return parseAddrErr- }- pb.HostIP = parsedIP- }- portBindings[port] = append(portBindings[port], pb)+ for port, bindings := range hostBindings {+ exposedPorts[port] = struct{}{}+ if len(bindings) > 0 {+ portBindings[port] = append(portBindings[port], bindings...)}}- return nil-}--func applyHostConfigSettings(hostConfig *dockercontainer.HostConfig, input *container.HostConfigCreate) {- if input == nil {- return- }-- if input.NetworkMode != "" {- hostConfig.NetworkMode = dockercontainer.NetworkMode(input.NetworkMode)- }- if input.Privileged != nil {- hostConfig.Privileged = *input.Privileged- }- if input.AutoRemove != nil {- hostConfig.AutoRemove = *input.AutoRemove- }- if input.ReadonlyRootfs != nil {- hostConfig.ReadonlyRootfs = *input.ReadonlyRootfs- }- if input.PublishAllPorts != nil {- hostConfig.PublishAllPorts = *input.PublishAllPorts- }- if input.RestartPolicy != nil {- hostConfig.RestartPolicy = dockercontainer.RestartPolicy{- Name: dockercontainer.RestartPolicyMode(input.RestartPolicy.Name),- MaximumRetryCount: input.RestartPolicy.MaximumRetryCount,+ endpoints := map[string]*network.EndpointSettings{}+ for name, endpoint := range kit.FromPtr(input.Body.NetworkingConfig).EndpointsConfig {+ ipam, ipamErr := endpointIPAMFromCreate(endpoint)+ if ipamErr != nil {+ return nil, huma.Error400BadRequest("Invalid network configuration: " + ipamErr.Error())}+ endpoints[name] = &network.EndpointSettings{Aliases: endpoint.Aliases, IPAMConfig: ipam}}- if input.Memory > 0 {- hostConfig.Memory = input.Memory- }- if input.MemorySwap > 0 {- hostConfig.MemorySwap = input.MemorySwap- }- if input.NanoCPUs > 0 {- hostConfig.NanoCPUs = input.NanoCPUs- }- if input.CPUShares > 0 {- hostConfig.CPUShares = input.CPUShares- }- if len(input.CapAdd) > 0 {- hostConfig.CapAdd = input.CapAdd- }- if len(input.CapDrop) > 0 {- hostConfig.CapDrop = input.CapDrop- }- if len(input.Mounts) > 0 {- hostConfig.Mounts = mountsFromCreateInternal(input.Mounts)- }-}--func applyHostConfigOverrides(body container.Create, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, portBindings network.PortMap) error {- if body.HostConfig == nil {- return nil- }-- if len(body.HostConfig.Binds) > 0 {- hostConfig.Binds = body.HostConfig.Binds- }-- if len(body.HostConfig.PortBindings) > 0 {- if err := applyHostConfigPortBindings(config, portBindings, body.HostConfig.PortBindings); err != nil {- return err+ if len(endpoints) == 0 {+ for _, networkName := range input.Body.Networks {+ endpoints[networkName] = &network.EndpointSettings{}}}- applyHostConfigSettings(hostConfig, body.HostConfig)- return nil-}--func applyLegacyResourceLimits(body container.Create, hostConfig *dockercontainer.HostConfig) {- if body.Memory > 0 {- hostConfig.Memory = body.Memory- }- if body.CPUs > 0 {- hostConfig.NanoCPUs = int64(body.CPUs * 1e9)- }-}--func buildNetworkingConfig(body container.Create) (*network.NetworkingConfig, error) {- if body.NetworkingConfig != nil && len(body.NetworkingConfig.EndpointsConfig) > 0 {- networkingConfig := &network.NetworkingConfig{EndpointsConfig: make(map[string]*network.EndpointSettings)}- for name, endpoint := range body.NetworkingConfig.EndpointsConfig {- ipam, err := endpointIPAMFromCreateInternal(endpoint)- if err != nil {- return nil, err+ createLabels := map[string]string{"com.arcane.created": "true"}+ maps.Copy(createLabels, input.Body.Labels)++ containerJSON, err := h.containerService.CreateContainer(+ ctx,+ &dockercontainer.Config{+ Image: input.Body.Image,+ Cmd: kit.Ternary(len(input.Body.Command) > 0, input.Body.Command, input.Body.Cmd),+ Entrypoint: input.Body.Entrypoint,+ WorkingDir: input.Body.WorkingDir,+ User: input.Body.User,+ Env: kit.Ternary(len(input.Body.Environment) > 0, input.Body.Environment, input.Body.Env),+ ExposedPorts: exposedPorts,+ Labels: createLabels,+ Healthcheck: healthConfigFromCreate(input.Body.Healthcheck),+ Hostname: input.Body.Hostname,+ Domainname: input.Body.Domainname,+ AttachStdout: input.Body.AttachStdout,+ AttachStderr: input.Body.AttachStderr,+ AttachStdin: input.Body.AttachStdin,+ Tty: input.Body.Tty,+ OpenStdin: input.Body.OpenStdin,+ StdinOnce: input.Body.StdinOnce,+ NetworkDisabled: input.Body.NetworkDisabled,+ },+ &dockercontainer.HostConfig{+ Binds: kit.Ternary(len(hostConfig.Binds) > 0, hostConfig.Binds, input.Body.Volumes),+ NetworkMode: dockercontainer.NetworkMode(hostConfig.NetworkMode),+ PortBindings: portBindings,+ RestartPolicy: dockercontainer.RestartPolicy{+ Name: dockercontainer.RestartPolicyMode(kit.Ternary(hostConfig.RestartPolicy != nil, kit.FromPtr(hostConfig.RestartPolicy).Name, input.Body.RestartPolicy)),+ MaximumRetryCount: kit.FromPtr(hostConfig.RestartPolicy).MaximumRetryCount,+ },+ AutoRemove: mo.PointerToOption(hostConfig.AutoRemove).OrElse(input.Body.AutoRemove),+ CapAdd: kit.Ternary(len(hostConfig.CapAdd) > 0, hostConfig.CapAdd, nil),+ CapDrop: kit.Ternary(len(hostConfig.CapDrop) > 0, hostConfig.CapDrop, nil),+ Privileged: mo.PointerToOption(hostConfig.Privileged).OrElse(input.Body.Privileged),+ PublishAllPorts: kit.FromPtr(hostConfig.PublishAllPorts),+ ReadonlyRootfs: kit.FromPtr(hostConfig.ReadonlyRootfs),+ Memory: kit.Ternary(input.Body.Memory > 0, input.Body.Memory, max(hostConfig.Memory, 0)),+ MemorySwap: max(hostConfig.MemorySwap, 0),+ NanoCPUs: kit.Ternary(input.Body.CPUs > 0, int64(input.Body.CPUs*1e9), max(hostConfig.NanoCPUs, 0)),+ CPUShares: max(hostConfig.CPUShares, 0),+ Mounts: kit.Ternary(len(hostConfig.Mounts) > 0, mergeEditMounts(nil, hostConfig.Mounts), nil),+ },+ func() *network.NetworkingConfig {+ if len(endpoints) == 0 {+ return nil}- networkingConfig.EndpointsConfig[name] = &network.EndpointSettings{Aliases: endpoint.Aliases, IPAMConfig: ipam}- }- return networkingConfig, nil- }-- if len(body.Networks) > 0 {- networkingConfig := &network.NetworkingConfig{EndpointsConfig: make(map[string]*network.EndpointSettings)}- for _, net := range body.Networks {- networkingConfig.EndpointsConfig[net] = &network.EndpointSettings{}- }- return networkingConfig, nil- }-- return nil, nil-}--func (h *ContainerHandler) CreateContainer(ctx context.Context, input *CreateContainerInput) (*handlerutil.Out[container.Created], error) {- user, err := handlerutil.RequireUser(ctx)- if err != nil {- return nil, err- }-- config := buildContainerConfig(input.Body)- portBindings := network.PortMap{}- if applyLegacyPortBindingsErr := applyLegacyPortBindings(input.Body, config, portBindings); applyLegacyPortBindingsErr != nil {- return nil, huma.Error400BadRequest("Invalid port format: " + applyLegacyPortBindingsErr.Error())- }- if applyExposedPortsErr := applyExposedPorts(input.Body.ExposedPorts, config); applyExposedPortsErr != nil {- return nil, huma.Error400BadRequest("Invalid port format: " + applyExposedPortsErr.Error())- }-- hostConfig := buildHostConfigBase(input.Body, portBindings)- if applyHostConfigOverridesErr := applyHostConfigOverrides(input.Body, config, hostConfig, portBindings); applyHostConfigOverridesErr != nil {- return nil, huma.Error400BadRequest("Invalid port format: " + applyHostConfigOverridesErr.Error())- }- applyLegacyResourceLimits(input.Body, hostConfig)-- networkingConfig, err := buildNetworkingConfig(input.Body)- if err != nil {- return nil, huma.Error400BadRequest("Invalid network configuration: " + err.Error())- }-- containerJSON, err := h.containerService.CreateContainer(ctx, config, hostConfig, networkingConfig, input.Body.Name, *user, input.Body.Credentials)+ return &network.NetworkingConfig{EndpointsConfig: endpoints}+ }(),+ input.Body.Name,+ *user,+ input.Body.Credentials,+ )if err != nil {return nil, huma.Error500InternalServerError("Failed to create container: " + err.Error())}- out := container.Created{- ID: containerJSON.ID,- Name: containerJSON.Name,- Image: containerJSON.Config.Image,- Status: string(containerJSON.State.Status),- Created: containerJSON.Created,- }-return &handlerutil.Out[container.Created]{Body: base.ApiResponse[container.Created]{Success: true,- Data: out,+ Data: container.Created{+ ID: containerJSON.ID,+ Name: containerJSON.Name,+ Image: containerJSON.Config.Image,+ Status: string(containerJSON.State.Status),+ Created: containerJSON.Created,+ },},}, nil}@@ -472,17 +354,8 @@ func (h *ContainerHandler) GetContainer(ctx context.Context, input *GetContainerfunc (h *ContainerHandler) GetContainerProcesses(ctx context.Context, input *GetContainerInput) (*handlerutil.Out[container.Processes], error) {processes, err := h.containerService.GetContainerProcesses(ctx, input.ContainerID)if err != nil {- message := "Failed to retrieve container processes: " + err.Error()- switch {- case errdefs.IsNotFound(err):- return nil, huma.Error404NotFound(message)- case errdefs.IsConflict(err):- return nil, huma.Error409Conflict(message)- case errors.Is(err, context.DeadlineExceeded):- return nil, huma.Error504GatewayTimeout(message)- default:- return nil, huma.Error500InternalServerError(message)- }+ apiErr := common.ToAPIError(err)+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to retrieve container processes: "+apiErr.Message)}return &handlerutil.Out[container.Processes]{@@ -567,7 +440,6 @@ func (h *ContainerHandler) RestartContainer(ctx context.Context, input *Containe}func (h *ContainerHandler) KillContainer(ctx context.Context, input *KillContainerInput) (*handlerutil.Out[base.MessageResponse], error) {- signal := strings.TrimSpace(input.Signal)return h.runContainerAction(ctx, &ContainerActionInput{EnvironmentID: input.EnvironmentID, ContainerID: input.ContainerID}, containerActionConfig{ActivityType: activitytypes.TypeContainerKill,Step: "Killing container",@@ -575,7 +447,7 @@ func (h *ContainerHandler) KillContainer(ctx context.Context, input *KillContainCompleteMessage: "Container killed",SuccessMessage: "Container killed successfully",Action: func(runtimeCtx context.Context, containerID string, user usertypes.Actor) error {- return h.containerService.KillContainer(runtimeCtx, containerID, signal, user)+ return h.containerService.KillContainer(runtimeCtx, containerID, strings.TrimSpace(input.Signal), user)},Error: func(err error) error {return huma.Error500InternalServerError("Failed to kill container: " + err.Error())@@ -615,16 +487,6 @@ func (h *ContainerHandler) UnpauseContainer(ctx context.Context, input *Containe})}-type containerActionConfig struct {- ActivityType activitytypes.Type- Step string- StartMessage string- CompleteMessage string- SuccessMessage string- Action func(context.Context, string, usertypes.Actor) error- Error func(error) error-}-// containerActivityName returns the container's name for activity records, falling back to its ID.func (h *ContainerHandler) containerActivityName(ctx context.Context, containerID string) string {name, err := h.containerService.GetContainerNameByID(ctx, containerID)@@ -732,25 +594,43 @@ func (h *ContainerHandler) RedeployContainer(ctx context.Context, input *Contain// Fetch full container details to return (consistent with other endpoints)details, inspectErr := h.containerService.GetContainerDetails(runtimeCtx, newContainerID)- if inspectErr == nil {- details.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer()-+ if inspectErr != nil {+ // Container was redeployed successfully, but we couldn't fetch full details.+ // Return minimal response with just the ID so frontend can still navigate.return &handlerutil.Out[container.Details]{Body: base.ApiResponse[container.Details]{Success: true,- Data: details,+ Data: container.Details{+ ID: newContainerID,+ ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),+ },},}, nil}- // Container was redeployed successfully, but we couldn't fetch full details.- // Return minimal response with just the ID so frontend can still navigate.return &handlerutil.Out[container.Details]{Body: base.ApiResponse[container.Details]{Success: true,- Data: container.Details{- ID: newContainerID,- ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),+ Data: container.Details{ //exhaustruct:enforce+ ID: details.ID,+ Name: details.Name,+ Image: details.Image,+ ImageID: details.ImageID,+ Created: details.Created,+ State: details.State,+ Config: details.Config,+ HostConfig: details.HostConfig,+ NetworkSettings: details.NetworkSettings,+ Ports: details.Ports,+ Mounts: details.Mounts,+ Labels: details.Labels,+ ComposeInfo: details.ComposeInfo,+ IconLightURL: details.IconLightURL,+ IconDarkURL: details.IconDarkURL,+ RedeployDisabled: details.RedeployDisabled,+ AutoUpdateEnabled: details.AutoUpdateEnabled,+ UpdateInfo: details.UpdateInfo,+ ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),},},}, nil@@ -770,17 +650,6 @@ func (h *ContainerHandler) GetContainerEditConfig(ctx context.Context, input *Ge}, nil}-func editContainerHTTPError(err error) error {- switch {- case errors.Is(err, common.ErrConflict):- return huma.Error409Conflict(err.Error())- case errors.Is(err, common.ErrValidation):- return huma.Error400BadRequest(err.Error())- default:- return huma.Error500InternalServerError("Failed to edit container: " + err.Error())- }-}-func (h *ContainerHandler) EditContainer(ctx context.Context, input *EditContainerInput) (*handlerutil.Out[container.Details], error) {user, err := handlerutil.RequireUser(ctx)if err != nil {@@ -814,32 +683,51 @@ func (h *ContainerHandler) EditContainer(ctx context.Context, input *EditContainif err != nil {activitylib.FlushWriter(activityWriter)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container edit failed", err)- return nil, editContainerHTTPError(err)+ apiErr := common.ToAPIError(err)+ return nil, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)}activitylib.FlushWriter(activityWriter)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container edited", nil)// Fetch full container details to return (consistent with redeploy)details, inspectErr := h.containerService.GetContainerDetails(runtimeCtx, newContainerID)- if inspectErr == nil {- details.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer()-+ if inspectErr != nil {+ // Container was recreated successfully, but we couldn't fetch full details.+ // Return minimal response with just the ID so frontend can still navigate.return &handlerutil.Out[container.Details]{Body: base.ApiResponse[container.Details]{Success: true,- Data: details,+ Data: container.Details{+ ID: newContainerID,+ ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),+ },},}, nil}- // Container was recreated successfully, but we couldn't fetch full details.- // Return minimal response with just the ID so frontend can still navigate.return &handlerutil.Out[container.Details]{Body: base.ApiResponse[container.Details]{Success: true,- Data: container.Details{- ID: newContainerID,- ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),+ Data: container.Details{ //exhaustruct:enforce+ ID: details.ID,+ Name: details.Name,+ Image: details.Image,+ ImageID: details.ImageID,+ Created: details.Created,+ State: details.State,+ Config: details.Config,+ HostConfig: details.HostConfig,+ NetworkSettings: details.NetworkSettings,+ Ports: details.Ports,+ Mounts: details.Mounts,+ Labels: details.Labels,+ ComposeInfo: details.ComposeInfo,+ IconLightURL: details.IconLightURL,+ IconDarkURL: details.IconDarkURL,+ RedeployDisabled: details.RedeployDisabled,+ AutoUpdateEnabled: details.AutoUpdateEnabled,+ UpdateInfo: details.UpdateInfo,+ ActivityID: mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer(),},},}, nil@@ -889,12 +777,9 @@ func (h *ContainerHandler) SetAutoUpdate(ctx context.Context, input *SetAutoUpdareturn nil, huma.Error404NotFound("container not found")}- excluded := !input.Body.Enabled- if setContainerAutoUpdateExclusionErr := h.settingsService.SetContainerAutoUpdateExclusionInternal(ctx, containerName, excluded); setContainerAutoUpdateExclusionErr != nil {+ if setContainerAutoUpdateExclusionErr := h.settingsService.SetContainerAutoUpdateExclusionInternal(ctx, containerName, !input.Body.Enabled); setContainerAutoUpdateExclusionErr != nil {return nil, huma.Error500InternalServerError("failed to update auto-update setting")}- msg := kit.Ternary(excluded, "Auto-update disabled", "Auto-update enabled")-- return handlerutil.MessageOutput(msg, ""), nil+ return handlerutil.MessageOutput(kit.Ternary(input.Body.Enabled, "Auto-update enabled", "Auto-update disabled"), ""), nil}diff --git a/backend/internal/container/module.go b/backend/internal/container/module.goindex 9dd18a039..fa1ce015a 100644--- a/backend/internal/container/module.go+++ b/backend/internal/container/module.go@@ -27,12 +27,15 @@ type Module struct {// New wires container routes around an existing service and registers per-container resource metrics once.func New(service *ContainerService, dockerService *docker.DockerClientService, settingsService *settings.SettingsService, activityService *activity.ActivityService) *Module {- if service != nil && service.stats != nil && service.dockerService != nil {- if err := service.stats.ObserveResources(otel.Meter(tracing.InstrumentationName), service.dockerService.ListContainers); err != nil {- otel.Handle(err)- }+ if err := service.stats.ObserveResources(otel.Meter(tracing.InstrumentationName), service.dockerService.ListContainers); err != nil {+ otel.Handle(err)+ }+ return &Module{+ service: service,+ dockerService: dockerService,+ settingsService: settingsService,+ activityService: activityService,}- return &Module{service: service, dockerService: dockerService, settingsService: settingsService, activityService: activityService}}// Service exposes the container service to collaborators.diff --git a/backend/internal/container/service.go b/backend/internal/container/service.goindex cb9f569fa..24354494b 100644--- a/backend/internal/container/service.go+++ b/backend/internal/container/service.go@@ -26,10 +26,11 @@ import ("github.com/moby/moby/api/types/network""github.com/moby/moby/client""github.com/samber/hot"+ "github.com/samber/mo""go.getarcane.app/docker""go.getarcane.app/docker/compat""go.getarcane.app/docker/types"- "go.getarcane.app/kit/pkg"+ kit "go.getarcane.app/kit/pkg""go.getarcane.app/sys/cgroup""go.getarcane.app/updater""go.getarcane.app/updater/labels"@@ -52,6 +53,12 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils/iconcatalog")+const (+ containerGroupByProject = "project"+ containerNoProjectGroup = "No Project"+ containerIconMetadataTTL = 5 * time.Second+)+type ContainerService struct {dockerService *dockerInternal.DockerClientServiceeventService *event.EventService@@ -62,12 +69,6 @@ type ContainerService struct {stats *stats.Service}-const (- containerGroupByProject = "project"- containerNoProjectGroup = "No Project"- containerIconMetadataTTL = 5 * time.Second-)-type ContainerListResult struct {Items []containertypes.SummaryGroups []containertypes.SummaryGroup@@ -75,6 +76,29 @@ type ContainerListResult struct {Counts containertypes.StatusCounts}+type containerLifecycleAction struct {+ action string+ eventType event.EventType+ metadata database.JSON+ warnOnLogError bool+ runContainerAction func(*client.Client) error+}++type ExecSession struct {+ execID string+ containerID string+ hijackedResp client.HijackedResponse+ dockerClient *client.Client+ closeOnce sync.Once+}++type demuxedLogs struct {+ io.Reader++ pipe *io.PipeReader+ logs io.ReadCloser+}+func NewContainerService(eventService *event.EventService,dockerService *dockerInternal.DockerClientService,@@ -96,20 +120,35 @@ func NewContainerService(}}-func buildCleanNetworkingConfigInternal(containerInspect container.InspectResponse, apiVersion string) *network.NetworkingConfig {- if containerInspect.NetworkSettings == nil || len(containerInspect.NetworkSettings.Networks) == 0 {+func buildCleanNetworkingConfig(containerInspect container.InspectResponse, apiVersion string) *network.NetworkingConfig {+ if len(kit.FromPtr(containerInspect.NetworkSettings).Networks) == 0 {return nil}- endpointsConfig := compat.SanitizeContainerCreateEndpointSettingsForDockerAPI(containerInspect.NetworkSettings.Networks, apiVersion)- for networkName, endpoint := range endpointsConfig {+ endpointsConfig := map[string]*network.EndpointSettings{}+ for networkName, endpoint := range compat.SanitizeContainerCreateEndpointSettingsForDockerAPI(containerInspect.NetworkSettings.Networks, apiVersion) {if endpoint == nil {+ endpointsConfig[networkName] = nilcontinue}- endpointCopy := *endpoint- endpointCopy.IPAMConfig = nil- endpointsConfig[networkName] = &endpointCopy+ endpointsConfig[networkName] = &network.EndpointSettings{ //exhaustruct:enforce+ IPAMConfig: nil,+ Links: endpoint.Links,+ Aliases: endpoint.Aliases,+ DriverOpts: endpoint.DriverOpts,+ GwPriority: endpoint.GwPriority,+ NetworkID: endpoint.NetworkID,+ EndpointID: endpoint.EndpointID,+ Gateway: endpoint.Gateway,+ IPAddress: endpoint.IPAddress,+ MacAddress: endpoint.MacAddress,+ IPPrefixLen: endpoint.IPPrefixLen,+ IPv6Gateway: endpoint.IPv6Gateway,+ GlobalIPv6Address: endpoint.GlobalIPv6Address,+ GlobalIPv6PrefixLen: endpoint.GlobalIPv6PrefixLen,+ DNSNames: endpoint.DNSNames,+ }}if len(endpointsConfig) == 0 {@@ -121,45 +160,14 @@ func buildCleanNetworkingConfigInternal(containerInspect container.InspectRespon}}-func buildRedeployBackupNameInternal(containerName, containerID string) string {- backupName := containerName- if backupName == "" {- backupName = "arcane-redeploy"- if len(containerID) >= 12 {- backupName = fmt.Sprintf("%s-%s", backupName, containerID[:12])- }- }-- return fmt.Sprintf("%s-arcane-redeploy-%d", backupName, time.Now().Unix())-}--func shouldStartRedeployedContainerInternal(containerInfo container.InspectResponse, wasRunning bool) bool {- if !wasRunning && containerInfo.HostConfig == nil {- return false- }-- shouldStart := wasRunning- if containerInfo.HostConfig != nil {- rp := containerInfo.HostConfig.RestartPolicy.Name- if rp == "always" || rp == "unless-stopped" || rp == "on-failure" {- shouldStart = true- }- }-- return shouldStart-}--func (- s *ContainerService,-) pullRedeployImageInternal(+func (s *ContainerService) pullRedeployImage(ctx context.Context,dockerClient *client.Client,imageName, containerID, containerName, action string,credentials []containerregistry.Credential,user usertypes.Actor,) error {- localSettings := s.settingsService.GetSettingsConfig()- pullCtx, pullCancel := context.WithTimeout(ctx, timeouts.GetDuration(localSettings.DockerImagePullTimeout.AsInt(), timeouts.DefaultDockerImagePull))+ pullCtx, pullCancel := context.WithTimeout(ctx, timeouts.GetDuration(s.settingsService.GetSettingsConfig().DockerImagePullTimeout.AsInt(), timeouts.DefaultDockerImagePull))defer pullCancel()pullOptions, authErr := s.imageService.PullOptionsWithAuth(ctx, imageName, credentials)@@ -219,9 +227,7 @@ func (return nil}-func (- s *ContainerService,-) prepareContainerForRedeployInternal(+func (s *ContainerService) prepareContainerForRedeploy(ctx context.Context,dockerClient *client.Client,containerID, containerName, backupName string,@@ -265,9 +271,7 @@ func (return fmt.Errorf("failed to stop container: %w", err)}-func (- s *ContainerService,-) restoreContainerAfterRedeployFailureInternal(+func (s *ContainerService) restoreContainerAfterRedeployFailure(ctx context.Context,dockerClient *client.Client,containerID, containerName, backupName, failedStep string,@@ -298,112 +302,7 @@ func (}}-// restoreAutoRemoveContainerAfterStartFailureInternal recreates and starts the-// original container when a deferred-stop auto-remove recreate fails to start-// the replacement. Stopping the original already made the daemon delete it, so-// the only rollback left is rebuilding from the inspected configuration. The-// failed replacement must be removed first so the original name is free again.-// A returned error means the original container is gone and could not be-// restored; the caller must surface it alongside the replacement failure.-func (- s *ContainerService,-) restoreAutoRemoveContainerAfterStartFailureInternal(- ctx context.Context,- dockerClient *client.Client,- containerInfo container.InspectResponse,- apiVersion string,- user usertypes.Actor,-) error {- containerID := containerInfo.ID- containerName := strings.TrimPrefix(containerInfo.Name, "/")-- originalConfig := *containerInfo.Config- if len(containerID) >= 12 && originalConfig.Hostname == containerID[:12] {- originalConfig.Hostname = ""- }-- defer s.eventService.BeginDockerResourceSuppressionWindow("container", "", containerName)()- createResp, err := compat.ContainerCreateWithCompatibilityForAPIVersion(ctx, dockerClient, client.ContainerCreateOptions{- Config: &originalConfig,- HostConfig: containerInfo.HostConfig,- NetworkingConfig: buildCleanNetworkingConfigInternal(containerInfo, apiVersion),- Name: containerName,- }, apiVersion)- if err != nil {- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, containerName, user.ID, user.Username, "0", err, database.JSON{- "action": "redeploy",- "step": "restore_create_original",- })- return fmt.Errorf("failed to recreate original auto-remove container: %w", err)- }-- defer s.eventService.BeginDockerResourceSuppressionWindow("container", createResp.ID, containerName)()- if _, containerStartErr := dockerClient.ContainerStart(ctx, createResp.ID, client.ContainerStartOptions{}); containerStartErr != nil {- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", createResp.ID, containerName, user.ID, user.Username, "0", containerStartErr, database.JSON{- "action": "redeploy",- "step": "restore_start_original",- })- return fmt.Errorf("failed to restart original auto-remove container: %w", containerStartErr)- }-- slog.InfoContext(- ctx, "restored auto-remove container after failed recreate",- "oldContainerId", containerID,- "restoredContainerId", createResp.ID,- "containerName", containerName,- )- return nil-}--// rollbackFailedReplacementStartInternal removes the replacement that failed-// to start and rolls back to the original container. For a deferred-stop-// auto-remove original the rollback rebuilds it from the inspected-// configuration; a failed restore is combined into the start error so the-// caller learns the workload is down, not just that the replacement failed.-func (- s *ContainerService,-) rollbackFailedReplacementStartInternal(- ctx context.Context,- dockerClient *client.Client,- containerInfo container.InspectResponse,- replacementID, action, backupName string,- stopAfterCreate, wasRunning bool,- apiVersion string,- startErr error,- user usertypes.Actor,-) error {- containerName := strings.TrimPrefix(containerInfo.Name, "/")-- s.eventService.MarkDockerExpectation("container", replacementID, containerName)- var cleanupErr error- if _, removeErr := dockerClient.ContainerRemove(ctx, replacementID, client.ContainerRemoveOptions{Force: true}); removeErr != nil {- cleanupErr = fmt.Errorf("failed to remove replacement container: %w", removeErr)- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", replacementID, containerName, user.ID, user.Username, "0", removeErr, database.JSON{- "action": action,- "step": "cleanup_failed_start",- })- }-- if stopAfterCreate {- if restoreErr := s.restoreAutoRemoveContainerAfterStartFailureInternal(ctx, dockerClient, containerInfo, apiVersion, user); restoreErr != nil {- return errors.Join(startErr, cleanupErr, restoreErr)- }- return startErr- }-- s.restoreContainerAfterRedeployFailureInternal(ctx, dockerClient, containerInfo.ID, containerName, backupName, "start", wasRunning, user)- return startErr-}--type containerLifecycleActionInternal struct {- action string- eventType event.EventType- metadata database.JSON- warnOnLogError bool- runContainerAction func(*client.Client) error-}--func (s *ContainerService) runContainerLifecycleActionInternal(ctx context.Context, containerID string, user usertypes.Actor, cfg containerLifecycleActionInternal) error {+func (s *ContainerService) runContainerLifecycleAction(ctx context.Context, containerID string, user usertypes.Actor, cfg containerLifecycleAction) error {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, "", user.ID, user.Username, "0", err, database.JSON{"action": cfg.action})@@ -433,7 +332,7 @@ func (s *ContainerService) runContainerLifecycleActionInternal(ctx context.Conte}func (s *ContainerService) StartContainer(ctx context.Context, containerID string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "start",eventType: event.EventTypeContainerStart,warnOnLogError: true,@@ -445,7 +344,7 @@ func (s *ContainerService) StartContainer(ctx context.Context, containerID strin}func (s *ContainerService) StopContainer(ctx context.Context, containerID string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "stop",eventType: event.EventTypeContainerStop,runContainerAction: func(dockerClient *client.Client) error {@@ -456,7 +355,7 @@ func (s *ContainerService) StopContainer(ctx context.Context, containerID string}func (s *ContainerService) RestartContainer(ctx context.Context, containerID string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "restart",eventType: event.EventTypeContainerRestart,runContainerAction: func(dockerClient *client.Client) error {@@ -469,7 +368,7 @@ func (s *ContainerService) RestartContainer(ctx context.Context, containerID str// KillContainer sends a signal to the container's main process (default SIGKILL// when signal is empty) without removing the container.func (s *ContainerService) KillContainer(ctx context.Context, containerID, signal string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "kill",eventType: event.EventTypeContainerKill,metadata: database.JSON{"signal": signal},@@ -483,7 +382,7 @@ func (s *ContainerService) KillContainer(ctx context.Context, containerID, signa// PauseContainer suspends all processes in the container.func (s *ContainerService) PauseContainer(ctx context.Context, containerID string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "pause",eventType: event.EventTypeContainerPause,warnOnLogError: true,@@ -496,7 +395,7 @@ func (s *ContainerService) PauseContainer(ctx context.Context, containerID strin// UnpauseContainer resumes a previously paused container.func (s *ContainerService) UnpauseContainer(ctx context.Context, containerID string, user usertypes.Actor) error {- return s.runContainerLifecycleActionInternal(ctx, containerID, user, containerLifecycleActionInternal{+ return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{action: "unpause",eventType: event.EventTypeContainerUnpause,warnOnLogError: true,@@ -536,7 +435,7 @@ func (s *ContainerService) CommitContainer(ctx context.Context, containerID strireturn nil, fmt.Errorf("failed to commit container: %w", err)}- metadata := database.JSON{+ if logErr := s.eventService.LogImageEvent(ctx, event.EventTypeImageCommit, containerID, reference, user.ID, user.Username, "0", database.JSON{"action": "commit","containerId": containerID,"imageId": result.ID,@@ -544,121 +443,13 @@ func (s *ContainerService) CommitContainer(ctx context.Context, containerID stri"tag": tag,"reference": reference,"noPause": req.NoPause,- }- if logErr := s.eventService.LogImageEvent(ctx, event.EventTypeImageCommit, containerID, reference, user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log container commit action", "container", containerID, "image", result.ID, "error", logErr)}return &containertypes.CommitResult{ID: result.ID}, nil}-// tryRedeployViaComposeProjectInternal attempts to redeploy a compose-managed-// container by delegating to project.ProjectService.UpdateProjectServices, which loads-// the compose project with full project_directory / env-file / include context-// and runs pull/stop/up for just the target service.-//-// Return semantics:-// - handled=false: this container is not eligible for the compose path (no-// labels, project not registered in Arcane's DB, etc.). The caller should-// fall back to the standalone Docker-API redeploy.-// - handled=true, err==nil: compose path ran successfully; newContainerID is-// the ID of the recreated container (or the original ID if it couldn't be-// re-located by labels).-// - handled=true, err!=nil: compose path was attempted and failed. The-// caller MUST surface the error and MUST NOT fall back to the standalone-// path, which would clobber whatever partial state ComposeUp left behind.-func (- s *ContainerService,-) tryRedeployViaComposeProjectInternal(- ctx context.Context,- containerInfo container.InspectResponse,- containerID, containerName string,- user usertypes.Actor,-) (- string,- bool,- error,-) {- if s.projectService == nil || containerInfo.Config == nil {- return "", false, nil- }- containerLabels := containerInfo.Config.Labels- projectName := docker.ComposeProjectLabel(containerLabels)- serviceName := docker.ComposeServiceLabel(containerLabels)- if projectName == "" || serviceName == "" {- return "", false, nil- }-- proj, err := s.projectService.GetProjectByComposeName(ctx, projectName)- if err != nil {- // Distinguish "not found" (safe to fall back to standalone) from real DB- // errors (should surface so a transient failure doesn't silently recreate- // the container from stale cached config).- if errors.Is(err, common.ErrNotFound) {- slog.WarnContext(- ctx, "RedeployContainer: compose project not registered, falling back to standalone redeploy",- "containerId", containerID,- "project", projectName,- "service", serviceName,- )- return "", false, nil- }- return "", true, fmt.Errorf("failed to look up compose project %s: %w", projectName, err)- }- if proj == nil {- slog.WarnContext(- ctx, "RedeployContainer: compose project not registered, falling back to standalone redeploy",- "containerId", containerID,- "project", projectName,- "service", serviceName,- )- return "", false, nil- }-- slog.InfoContext(- ctx, "RedeployContainer: detected compose container, using project-based redeploy",- "containerId", containerID,- "project", projectName,- "service", serviceName,- )-- if updateProjectServicesErr := s.projectService.UpdateProjectServices(ctx, proj.ID, []string{serviceName}, user, false); updateProjectServicesErr != nil {- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, containerName, user.ID, user.Username, "0", updateProjectServicesErr, database.JSON{- "action": "redeploy",- "step": "compose_update_services",- "project": projectName,- "service": serviceName,- "projectId": proj.ID,- "projectName": proj.Name,- })- return "", true, fmt.Errorf("compose redeploy failed for %s/%s: %w", projectName, serviceName, updateProjectServicesErr)- }-- newID, lookupErr := projects.FindComposeServiceContainerID(ctx, s.dockerService.DockerHost(), projectName, serviceName)- if lookupErr != nil {- slog.WarnContext(ctx, "failed to resolve container via compose ps after redeploy", "project", projectName, "service", serviceName, "err", lookupErr)- }- if newID == "" {- // Recreated successfully but couldn't locate the new container; return the- // original ID so the handler can degrade gracefully.- newID = containerID- }-- if logErr := s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDeploy, newID, containerName, user.ID, user.Username, "0", database.JSON{- "action": "redeploy",- "containerId": newID,- "containerName": containerName,- "project": projectName,- "service": serviceName,- "projectId": proj.ID,- "via": "compose",- }); logErr != nil {- slog.WarnContext(ctx, "failed to log compose redeploy event", "err", logErr)- }-- return newID, true, nil-}-func (s *ContainerService) RedeployContainer(ctx context.Context, containerID string, user usertypes.Actor) (string, error) {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {@@ -689,7 +480,6 @@ func (s *ContainerService) RedeployContainer(ctx context.Context, containerID st}containerName := strings.TrimPrefix(containerInfo.Name, "/")- imageName := containerInfo.Config.ImageapiVersion := compat.DetectDockerAPIVersion(ctx, dockerClient)currentContainerID, currentContainerErr := cgroup.CurrentContainerID()@@ -702,48 +492,87 @@ func (s *ContainerService) RedeployContainer(ctx context.Context, containerID streturn "", err}- // If this container belongs to a known compose project, redeploy through the- // compose-aware path so that compose file changes (healthchecks, env, etc.) and- // the project's include/project_directory/env-file context are honored. The- // standalone Docker-API path below only clones the existing container config- // from the daemon and would silently ignore any compose edits.- if newID, handled, composeErr := s.tryRedeployViaComposeProjectInternal(ctx, containerInfo, containerID, containerName, user); handled {- if composeErr != nil {- return "", composeErr+ // A container in a known compose project redeploys through project.ProjectService.UpdateProjectServices so compose+ // edits and include/env-file context are honored; the standalone path below only clones the daemon's config.+ if projectName, serviceName := docker.ComposeProjectLabel(containerInfo.Config.Labels), docker.ComposeServiceLabel(containerInfo.Config.Labels); projectName != "" && serviceName != "" {+ proj, projectErr := s.projectService.GetProjectByComposeName(ctx, projectName)+ // Real DB errors surface so a transient failure doesn't silently recreate from stale cached config.+ if projectErr != nil && !errors.Is(projectErr, common.ErrNotFound) {+ return "", fmt.Errorf("failed to look up compose project %s: %w", projectName, projectErr)}- return newID, nil+ if projectErr == nil {+ slog.InfoContext(+ ctx, "RedeployContainer: detected compose container, using project-based redeploy",+ "containerId", containerID,+ "project", projectName,+ "service", serviceName,+ )++ if updateProjectServicesErr := s.projectService.UpdateProjectServices(ctx, proj.ID, []string{serviceName}, user, false); updateProjectServicesErr != nil {+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, containerName, user.ID, user.Username, "0", updateProjectServicesErr, database.JSON{+ "action": "redeploy",+ "step": "compose_update_services",+ "project": projectName,+ "service": serviceName,+ "projectId": proj.ID,+ "projectName": proj.Name,+ })+ return "", fmt.Errorf("compose redeploy failed for %s/%s: %w", projectName, serviceName, updateProjectServicesErr)+ }++ newID, lookupErr := projects.FindComposeServiceContainerID(ctx, s.dockerService.DockerHost(), projectName, serviceName)+ if lookupErr != nil {+ slog.WarnContext(ctx, "failed to resolve container via compose ps after redeploy", "project", projectName, "service", serviceName, "err", lookupErr)+ }++ // An unlocated replacement falls back to the original ID so the handler can degrade gracefully.+ if logErr := s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDeploy, cmp.Or(newID, containerID), containerName, user.ID, user.Username, "0", database.JSON{+ "action": "redeploy",+ "containerId": cmp.Or(newID, containerID),+ "containerName": containerName,+ "project": projectName,+ "service": serviceName,+ "projectId": proj.ID,+ "via": "compose",+ }); logErr != nil {+ slog.WarnContext(ctx, "failed to log compose redeploy event", "err", logErr)+ }++ return cmp.Or(newID, containerID), nil+ }++ slog.WarnContext(+ ctx, "RedeployContainer: compose project not registered, falling back to standalone redeploy",+ "containerId", containerID,+ "project", projectName,+ "service", serviceName,+ )}- if imageName != "" {- if pullRedeployImageErr := s.pullRedeployImageInternal(ctx, dockerClient, imageName, containerID, containerName, "redeploy", nil, user); pullRedeployImageErr != nil {+ if containerInfo.Config.Image != "" {+ if pullRedeployImageErr := s.pullRedeployImage(ctx, dockerClient, containerInfo.Config.Image, containerID, containerName, "redeploy", nil, user); pullRedeployImageErr != nil {return "", pullRedeployImageErr}}- networkingConfig := buildCleanNetworkingConfigInternal(containerInfo, apiVersion)- newConfig := *containerInfo.Config-- return s.recreateContainerInternal(+ return s.recreateContainer(ctx,dockerClient,containerInfo,"redeploy",containerName,- &newConfig,+ new(*containerInfo.Config),containerInfo.HostConfig,- networkingConfig,+ buildCleanNetworkingConfig(containerInfo, apiVersion),apiVersion,event.EventTypeContainerDeploy,user,)}-// recreateContainerInternal replaces an existing container with one created-// from the supplied config, using rename-as-backup so the original can be-// restored when create or start fails. Shared by redeploy and edit.-func (- s *ContainerService,-) recreateContainerInternal(+// recreateContainer replaces an existing container with one created from the supplied config, using+// rename-as-backup so the original can be restored when create or start fails. Shared by redeploy and edit.+func (s *ContainerService) recreateContainer(ctx context.Context,dockerClient *client.Client,containerInfo container.InspectResponse,@@ -758,26 +587,68 @@ func (string,error,) {- containerID := containerInfo.IDcontainerName := strings.TrimPrefix(containerInfo.Name, "/")- wasRunning := containerInfo.State != nil && containerInfo.State.Running- imageName := newConfig.Image-- // A running auto-remove container is deleted by the daemon the moment it- // stops, which would make rollback impossible. For that case the stop is- // deferred until the replacement has been created: the rename alone frees- // the name, and a create failure rolls back to the untouched, still-running- // original. Only a start failure can still lose the original.- stopAfterCreate := wasRunning && containerInfo.HostConfig != nil && containerInfo.HostConfig.AutoRemove-- defer s.eventService.BeginDockerResourceSuppressionWindow("container", containerID, containerName)()- backupName := buildRedeployBackupNameInternal(containerName, containerID)- if err := s.prepareContainerForRedeployInternal(ctx, dockerClient, containerID, containerName, backupName, wasRunning && !stopAfterCreate, user); err != nil {+ wasRunning := kit.FromPtr(containerInfo.State).Running++ // A running auto-remove container is deleted by the daemon the moment it stops, so its stop is+ // deferred until the replacement exists; only a start failure can still lose the original.+ stopAfterCreate := wasRunning && kit.FromPtr(containerInfo.HostConfig).AutoRemove++ defer s.eventService.BeginDockerResourceSuppressionWindow("container", containerInfo.ID, containerName)()+ backupName := fmt.Sprintf("%s-arcane-redeploy-%d", cmp.Or(containerName, func() string {+ if len(containerInfo.ID) < 12 {+ return "arcane-redeploy"+ }+ return "arcane-redeploy-" + containerInfo.ID[:12]+ }()), time.Now().Unix())++ // Stopping the original made the daemon delete it, so rebuild it from the inspected config.+ // A returned error means the workload is down and is joined into the start error.+ restoreAutoRemoveOriginal := func() error {+ originalConfig := *containerInfo.Config+ if len(containerInfo.ID) >= 12 && originalConfig.Hostname == containerInfo.ID[:12] {+ originalConfig.Hostname = "" // keep: copying Docker's 24-field container.Config to clear one field is unreadable+ }++ defer s.eventService.BeginDockerResourceSuppressionWindow("container", "", containerName)()+ restoreResp, restoreCreateErr := compat.ContainerCreateWithCompatibilityForAPIVersion(ctx, dockerClient, client.ContainerCreateOptions{+ Config: &originalConfig,+ HostConfig: containerInfo.HostConfig,+ NetworkingConfig: buildCleanNetworkingConfig(containerInfo, apiVersion),+ Name: containerName,+ }, apiVersion)+ if restoreCreateErr != nil {+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerInfo.ID, containerName, user.ID, user.Username, "0", restoreCreateErr, database.JSON{+ "action": "redeploy",+ "step": "restore_create_original",+ })+ return fmt.Errorf("failed to recreate original auto-remove container: %w", restoreCreateErr)+ }++ defer s.eventService.BeginDockerResourceSuppressionWindow("container", restoreResp.ID, containerName)()+ if _, restoreStartErr := dockerClient.ContainerStart(ctx, restoreResp.ID, client.ContainerStartOptions{}); restoreStartErr != nil {+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", restoreResp.ID, containerName, user.ID, user.Username, "0", restoreStartErr, database.JSON{+ "action": "redeploy",+ "step": "restore_start_original",+ })+ return fmt.Errorf("failed to restart original auto-remove container: %w", restoreStartErr)+ }++ slog.InfoContext(+ ctx, "restored auto-remove container after failed recreate",+ "oldContainerId", containerInfo.ID,+ "restoredContainerId", restoreResp.ID,+ "containerName", containerName,+ )+ return nil+ }++ if err := s.prepareContainerForRedeploy(ctx, dockerClient, containerInfo.ID, containerName, backupName, wasRunning && !stopAfterCreate, user); err != nil {return "", err}- if len(containerID) >= 12 && newConfig.Hostname == containerID[:12] {- newConfig.Hostname = ""+ if len(containerInfo.ID) >= 12 && newConfig.Hostname == containerInfo.ID[:12] {+ newConfig.Hostname = "" // keep: copying Docker's 24-field container.Config to clear one field is unreadable}defer s.eventService.BeginDockerResourceSuppressionWindow("container", "", newName)()@@ -788,11 +659,11 @@ func (Name: newName,}, apiVersion)if err != nil {- s.restoreContainerAfterRedeployFailureInternal(ctx, dockerClient, containerID, containerName, backupName, "create", wasRunning && !stopAfterCreate, user)- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, containerName, user.ID, user.Username, "0", err, database.JSON{+ s.restoreContainerAfterRedeployFailure(ctx, dockerClient, containerInfo.ID, containerName, backupName, "create", wasRunning && !stopAfterCreate, user)+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerInfo.ID, containerName, user.ID, user.Username, "0", err, database.JSON{"action": action,"step": "create",- "image": imageName,+ "image": newConfig.Image,})return "", fmt.Errorf("failed to recreate container: %w", err)}@@ -800,15 +671,15 @@ func (defer s.eventService.BeginDockerResourceSuppressionWindow("container", createResp.ID, newName)()if stopAfterCreate {- if _, containerStopErr := dockerClient.ContainerStop(ctx, containerID, client.ContainerStopOptions{Timeout: new(30)}); containerStopErr != nil {+ if _, containerStopErr := dockerClient.ContainerStop(ctx, containerInfo.ID, client.ContainerStopOptions{Timeout: new(30)}); containerStopErr != nil {if _, removeErr := dockerClient.ContainerRemove(ctx, createResp.ID, client.ContainerRemoveOptions{Force: true}); removeErr != nil {s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", createResp.ID, containerName, user.ID, user.Username, "0", removeErr, database.JSON{"action": action,"step": "cleanup_failed_stop",})}- s.restoreContainerAfterRedeployFailureInternal(ctx, dockerClient, containerID, containerName, backupName, "stop", false, user)- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerID, containerName, user.ID, user.Username, "0", containerStopErr, database.JSON{+ s.restoreContainerAfterRedeployFailure(ctx, dockerClient, containerInfo.ID, containerName, backupName, "stop", false, user)+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerInfo.ID, containerName, user.ID, user.Username, "0", containerStopErr, database.JSON{"action": action,"step": "stop",})@@ -816,14 +687,30 @@ func (}}- if shouldStartRedeployedContainerInternal(containerInfo, wasRunning) {- _, err = dockerClient.ContainerStart(ctx, createResp.ID, client.ContainerStartOptions{})- if err != nil {- err = s.rollbackFailedReplacementStartInternal(ctx, dockerClient, containerInfo, createResp.ID, action, backupName, stopAfterCreate, wasRunning, apiVersion, err, user)+ restartPolicies := []container.RestartPolicyMode{container.RestartPolicyAlways, container.RestartPolicyUnlessStopped, container.RestartPolicyOnFailure}+ if wasRunning || slices.Contains(restartPolicies, kit.FromPtr(containerInfo.HostConfig).RestartPolicy.Name) {+ if _, err = dockerClient.ContainerStart(ctx, createResp.ID, client.ContainerStartOptions{}); err != nil {+ // Remove the replacement and roll back to the original. A deferred-stop auto-remove original+ // is rebuilt from its inspected config; a failed restore joins the start error.+ s.eventService.MarkDockerExpectation("container", createResp.ID, containerName)+ _, removeErr := dockerClient.ContainerRemove(ctx, createResp.ID, client.ContainerRemoveOptions{Force: true})+ if removeErr != nil {+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", createResp.ID, containerName, user.ID, user.Username, "0", removeErr, database.JSON{+ "action": action,+ "step": "cleanup_failed_start",+ })+ }+ if stopAfterCreate {+ if restoreErr := restoreAutoRemoveOriginal(); restoreErr != nil {+ err = errors.Join(err, kit.Ternary(removeErr != nil, fmt.Errorf("failed to remove replacement container: %w", removeErr), nil), restoreErr)+ }+ } else {+ s.restoreContainerAfterRedeployFailure(ctx, dockerClient, containerInfo.ID, containerName, backupName, "start", wasRunning, user)+ }s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", createResp.ID, containerName, user.ID, user.Username, "0", err, database.JSON{"action": action,"step": "start",- "image": imageName,+ "image": newConfig.Image,})return "", fmt.Errorf("failed to start new container: %w", err)}@@ -832,42 +719,41 @@ func (slog.InfoContext(ctx, "container recreated successfully","action", action,- "oldContainerId", containerID,+ "oldContainerId", containerInfo.ID,"newContainerId", createResp.ID,"containerName", newName,- "image", imageName,+ "image", newConfig.Image,)// After a deferred stop the daemon has already auto-removed the original.if !stopAfterCreate {- if _, containerRemoveErr := dockerClient.ContainerRemove(ctx, containerID, client.ContainerRemoveOptions{+ if _, containerRemoveErr := dockerClient.ContainerRemove(ctx, containerInfo.ID, client.ContainerRemoveOptions{Force: true,RemoveVolumes: false,RemoveLinks: false,}); containerRemoveErr != nil {slog.WarnContext(ctx, "failed to remove old container after successful recreate",- "containerId", containerID,+ "containerId", containerInfo.ID,"backupName", backupName,"error", containerRemoveErr,)}}- metadata := database.JSON{+ if logErr := s.eventService.LogContainerEvent(ctx, eventType, createResp.ID, newName, user.ID, user.Username, "0", database.JSON{"action": action,- "containerId": containerID,+ "containerId": containerInfo.ID,"containerName": newName,- "image": imageName,- }- if logErr := s.eventService.LogContainerEvent(ctx, eventType, createResp.ID, newName, user.ID, user.Username, "0", metadata); logErr != nil {+ "image": newConfig.Image,+ }); logErr != nil {slog.WarnContext(ctx, "failed to log recreate event", "err", logErr)}return createResp.ID, nil}-func healthConfigFromCreateInternal(hc *containertypes.HealthcheckCreate) *container.HealthConfig {+func healthConfigFromCreate(hc *containertypes.HealthcheckCreate) *container.HealthConfig {if hc == nil {return nil}@@ -882,247 +768,187 @@ func healthConfigFromCreateInternal(hc *containertypes.HealthcheckCreate) *conta}}-// mergeEditMountsInternal builds the desired mount set for an edit. The edit-// form only carries the reduced {type, source, target, readOnly} view, so a-// requested mount that matches an existing one by type and target keeps the-// full original spec (bind/volume/tmpfs options, consistency, ...) with the-// source and read-only flag applied from the request; unmatched entries are-// new mounts built from the request.-func mergeEditMountsInternal(existing []mount.Mount, requested []containertypes.MountCreate) []mount.Mount {- out := make([]mount.Mount, 0, len(requested))- for _, req := range requested {- merged := mount.Mount{- Type: mount.Type(req.Type),- Source: req.Source,- Target: req.Target,- ReadOnly: req.ReadOnly,+// mergeEditMounts builds the desired mount set for an edit. A requested mount matching an existing one by type+// and target keeps its full original spec with the request's source and read-only flag; others are built fresh.+func mergeEditMounts(existing []mount.Mount, requested []containertypes.MountCreate) []mount.Mount {+ priorByKey := make(map[string]mount.Mount, len(existing))+ for _, prior := range existing {+ if _, seen := priorByKey[string(prior.Type)+"\x00"+prior.Target]; !seen {+ priorByKey[string(prior.Type)+"\x00"+prior.Target] = prior}- for _, prior := range existing {- if string(prior.Type) == req.Type && prior.Target == req.Target {- merged = prior- merged.Source = req.Source- merged.ReadOnly = req.ReadOnly- break- }- }- out = append(out, merged)}- return out-}-func mountsFromCreateInternal(mounts []containertypes.MountCreate) []mount.Mount {- out := make([]mount.Mount, 0, len(mounts))- for _, m := range mounts {- out = append(out, mount.Mount{- Type: mount.Type(m.Type),- Source: m.Source,- Target: m.Target,- ReadOnly: m.ReadOnly,+ out := make([]mount.Mount, 0, len(requested))+ for _, req := range requested {+ prior := priorByKey[req.Type+"\x00"+req.Target]+ out = append(out, mount.Mount{ //exhaustruct:enforce+ Type: mount.Type(req.Type),+ Source: req.Source,+ Target: req.Target,+ ReadOnly: req.ReadOnly,+ Consistency: prior.Consistency,+ BindOptions: prior.BindOptions,+ VolumeOptions: prior.VolumeOptions,+ ImageOptions: prior.ImageOptions,+ TmpfsOptions: prior.TmpfsOptions,+ ClusterOptions: prior.ClusterOptions,})}return out}-func portMapFromCreateInternal(bindings map[string][]containertypes.PortBindingCreate) (network.PortMap, error) {+func portMapFromCreate(bindings map[string][]containertypes.PortBindingCreate) (network.PortMap, error) {out := network.PortMap{}for portSpec, bindingList := range bindings {- port, err := parsePortSpec(portSpec)+ port, err := network.ParsePort(kit.Ternary(strings.Contains(portSpec, "/"), portSpec, portSpec+"/tcp"))if err != nil {return nil, fmt.Errorf("invalid port %s: %w", portSpec, err)}mapped := make([]network.PortBinding, 0, len(bindingList))for _, binding := range bindingList {- pb := network.PortBinding{HostPort: binding.HostPort}- if hostIP := strings.TrimSpace(binding.HostIP); hostIP != "" {- parsedIP, parseAddrErr := netip.ParseAddr(hostIP)- if parseAddrErr != nil {- return nil, fmt.Errorf("invalid host IP %s: %w", hostIP, parseAddrErr)- }- pb.HostIP = parsedIP+ hostIP, parseAddrErr := netip.ParseAddr(strings.TrimSpace(binding.HostIP))+ if parseAddrErr != nil && strings.TrimSpace(binding.HostIP) != "" {+ return nil, fmt.Errorf("invalid host IP %s: %w", strings.TrimSpace(binding.HostIP), parseAddrErr)}- mapped = append(mapped, pb)+ mapped = append(mapped, network.PortBinding{HostIP: hostIP, HostPort: binding.HostPort})}out[port] = mapped}return out, nil}-func endpointIPAMFromCreateInternal(ep containertypes.EndpointSettingsCreate) (*network.EndpointIPAMConfig, error) {- ipv4 := strings.TrimSpace(ep.IPv4Address)- ipv6 := strings.TrimSpace(ep.IPv6Address)- if ipv4 == "" && ipv6 == "" {+func endpointIPAMFromCreate(ep containertypes.EndpointSettingsCreate) (*network.EndpointIPAMConfig, error) {+ if strings.TrimSpace(ep.IPv4Address) == "" && strings.TrimSpace(ep.IPv6Address) == "" {return nil, nil}- cfg := &network.EndpointIPAMConfig{}- if ipv4 != "" {- addr, err := netip.ParseAddr(ipv4)- if err != nil {- return nil, fmt.Errorf("invalid IPv4 address %s: %w", ipv4, err)- }- cfg.IPv4Address = addr+ ipv4, ipv4Err := netip.ParseAddr(strings.TrimSpace(ep.IPv4Address))+ if ipv4Err != nil && strings.TrimSpace(ep.IPv4Address) != "" {+ return nil, fmt.Errorf("invalid IPv4 address %s: %w", strings.TrimSpace(ep.IPv4Address), ipv4Err)}- if ipv6 != "" {- addr, err := netip.ParseAddr(ipv6)- if err != nil {- return nil, fmt.Errorf("invalid IPv6 address %s: %w", ipv6, err)- }- cfg.IPv6Address = addr+ ipv6, ipv6Err := netip.ParseAddr(strings.TrimSpace(ep.IPv6Address))+ if ipv6Err != nil && strings.TrimSpace(ep.IPv6Address) != "" {+ return nil, fmt.Errorf("invalid IPv6 address %s: %w", strings.TrimSpace(ep.IPv6Address), ipv6Err)}- return cfg, nil+ return &network.EndpointIPAMConfig{IPv4Address: ipv4, IPv6Address: ipv6}, nil}-// applyEditToContainerConfigInternal overwrites the non-nil form-owned Config-// sections of an edit request onto cfg. Settings the form does not own are-// left untouched. When port bindings are replaced, ExposedPorts is recomputed-// as the new binding keys plus any previously-exposed-but-unbound ports (image-// EXPOSE entries survive).-func applyEditToContainerConfigInternal(cfg *container.Config, previousBindings network.PortMap, req containertypes.Edit) error {- if req.Image != nil && strings.TrimSpace(*req.Image) != "" {- cfg.Image = strings.TrimSpace(*req.Image)- }- if req.WorkingDir != nil {- cfg.WorkingDir = *req.WorkingDir- }- if req.User != nil {- cfg.User = *req.User- }- if req.Command != nil {- cfg.Cmd = append([]string{}, *req.Command...)- }- if req.Entrypoint != nil {- cfg.Entrypoint = append([]string{}, *req.Entrypoint...)- }- if req.Environment != nil {- cfg.Env = append([]string{}, *req.Environment...)- }- if req.Labels != nil {- cfg.Labels = maps.Clone(*req.Labels)+// applyEditToContainerConfig returns cfg with the non-nil form-owned sections of an edit applied. Replaced port+// bindings recompute ExposedPorts as the new keys plus previously exposed but unbound ports (image EXPOSE entries).+func applyEditToContainerConfig(cfg container.Config, previousBindings network.PortMap, req containertypes.Edit) (container.Config, error) {+ newBindings, err := portMapFromCreate(kit.FromPtr(kit.FromPtr(req.HostConfig).PortBindings))+ if err != nil {+ return container.Config{}, err}-- switch {- case req.ClearHealthcheck:- cfg.Healthcheck = nil- case req.Healthcheck != nil:- cfg.Healthcheck = healthConfigFromCreateInternal(req.Healthcheck)+ exposed := network.PortSet{}+ for port := range newBindings {+ exposed[port] = struct{}{}}-- if req.HostConfig != nil && req.HostConfig.PortBindings != nil {- newBindings, err := portMapFromCreateInternal(*req.HostConfig.PortBindings)- if err != nil {- return err- }- exposed := network.PortSet{}- for port := range newBindings {+ for port := range cfg.ExposedPorts {+ if _, wasBound := previousBindings[port]; !wasBound {exposed[port] = struct{}{}}- for port := range cfg.ExposedPorts {- if _, wasBound := previousBindings[port]; !wasBound {- exposed[port] = struct{}{}- }- }- cfg.ExposedPorts = exposed}- return nil+ return container.Config{ //exhaustruct:enforce+ Hostname: cfg.Hostname,+ Domainname: cfg.Domainname,+ User: mo.PointerToOption(req.User).OrElse(cfg.User),+ AttachStdin: cfg.AttachStdin,+ AttachStdout: cfg.AttachStdout,+ AttachStderr: cfg.AttachStderr,+ ExposedPorts: kit.Ternary(kit.FromPtr(req.HostConfig).PortBindings != nil, exposed, cfg.ExposedPorts),+ Tty: cfg.Tty,+ OpenStdin: cfg.OpenStdin,+ StdinOnce: cfg.StdinOnce,+ Env: kit.Ternary(req.Environment != nil, append([]string{}, kit.FromPtr(req.Environment)...), cfg.Env),+ Cmd: kit.Ternary(req.Command != nil, append([]string{}, kit.FromPtr(req.Command)...), cfg.Cmd),+ Healthcheck: kit.Ternary(req.ClearHealthcheck, nil, cmp.Or(healthConfigFromCreate(req.Healthcheck), cfg.Healthcheck)),+ ArgsEscaped: cfg.ArgsEscaped,+ Image: cmp.Or(strings.TrimSpace(kit.FromPtr(req.Image)), cfg.Image),+ Volumes: cfg.Volumes,+ WorkingDir: mo.PointerToOption(req.WorkingDir).OrElse(cfg.WorkingDir),+ Entrypoint: kit.Ternary(req.Entrypoint != nil, append([]string{}, kit.FromPtr(req.Entrypoint)...), cfg.Entrypoint),+ NetworkDisabled: cfg.NetworkDisabled,+ OnBuild: cfg.OnBuild,+ Labels: kit.Ternary(req.Labels != nil, maps.Clone(kit.FromPtr(req.Labels)), cfg.Labels),+ StopSignal: cfg.StopSignal,+ StopTimeout: cfg.StopTimeout,+ Shell: cfg.Shell,+ }, nil}-// applyEditToHostConfigInternal overwrites the non-nil form-owned HostConfig-// sections of an edit request onto hc. Everything else (sysctls, ulimits, log-// drivers, devices, tmpfs, DNS, ...) carries over unchanged.-func applyEditToHostConfigInternal(hc *container.HostConfig, req containertypes.Edit) error {- edit := req.HostConfig- if edit == nil {+// applyEditToHostConfig overwrites the non-nil form-owned HostConfig sections of an edit request onto hc.+// Everything else (sysctls, ulimits, log drivers, devices, tmpfs, DNS, ...) carries over unchanged.+func applyEditToHostConfig(hc *container.HostConfig, req containertypes.Edit) error {+ if req.HostConfig == nil {return nil}- if edit.Binds != nil {- hc.Binds = append([]string{}, *edit.Binds...)- }- if edit.Mounts != nil {- hc.Mounts = mergeEditMountsInternal(hc.Mounts, *edit.Mounts)- }- if edit.PortBindings != nil {- newBindings, err := portMapFromCreateInternal(*edit.PortBindings)- if err != nil {- return err- }- hc.PortBindings = newBindings+ newBindings, err := portMapFromCreate(kit.FromPtr(req.HostConfig.PortBindings))+ if err != nil {+ return err}- if edit.RestartPolicy != nil {- hc.RestartPolicy = container.RestartPolicy{- Name: container.RestartPolicyMode(edit.RestartPolicy.Name),- MaximumRetryCount: edit.RestartPolicy.MaximumRetryCount,++ if req.HostConfig.RestartPolicy != nil {+ hc.RestartPolicy = container.RestartPolicy{ // keep: Docker's ~70-field container.HostConfig is unreadable as a literal+ Name: container.RestartPolicyMode(req.HostConfig.RestartPolicy.Name),+ MaximumRetryCount: req.HostConfig.RestartPolicy.MaximumRetryCount,}}- if edit.Privileged != nil {- hc.Privileged = *edit.Privileged- }- if edit.CapAdd != nil {- hc.CapAdd = append([]string{}, *edit.CapAdd...)- }- if edit.CapDrop != nil {- hc.CapDrop = append([]string{}, *edit.CapDrop...)- }- if edit.AutoRemove != nil {- hc.AutoRemove = *edit.AutoRemove- }- if edit.ReadonlyRootfs != nil {- hc.ReadonlyRootfs = *edit.ReadonlyRootfs- }- if edit.Memory != nil {- hc.Memory = *edit.Memory- }- if edit.MemorySwap != nil {- hc.MemorySwap = *edit.MemorySwap- }- if edit.NanoCPUs != nil {- hc.NanoCPUs = *edit.NanoCPUs- }- if edit.CPUShares != nil {- hc.CPUShares = *edit.CPUShares- }+ hc.Binds = kit.Ternary(req.HostConfig.Binds != nil, append([]string{}, kit.FromPtr(req.HostConfig.Binds)...), hc.Binds) // keep: ~70-field HostConfig+ hc.Mounts = kit.Ternary(req.HostConfig.Mounts != nil, mergeEditMounts(hc.Mounts, kit.FromPtr(req.HostConfig.Mounts)), hc.Mounts) // keep: ~70-field HostConfig+ hc.PortBindings = kit.Ternary(req.HostConfig.PortBindings != nil, newBindings, hc.PortBindings) // keep: ~70-field HostConfig+ hc.Privileged = mo.PointerToOption(req.HostConfig.Privileged).OrElse(hc.Privileged) // keep: ~70-field HostConfig+ hc.CapAdd = kit.Ternary(req.HostConfig.CapAdd != nil, append([]string{}, kit.FromPtr(req.HostConfig.CapAdd)...), hc.CapAdd) // keep: ~70-field HostConfig+ hc.CapDrop = kit.Ternary(req.HostConfig.CapDrop != nil, append([]string{}, kit.FromPtr(req.HostConfig.CapDrop)...), hc.CapDrop) // keep: ~70-field HostConfig+ hc.AutoRemove = mo.PointerToOption(req.HostConfig.AutoRemove).OrElse(hc.AutoRemove) // keep: ~70-field HostConfig+ hc.ReadonlyRootfs = mo.PointerToOption(req.HostConfig.ReadonlyRootfs).OrElse(hc.ReadonlyRootfs) // keep: ~70-field HostConfig+ hc.Memory = mo.PointerToOption(req.HostConfig.Memory).OrElse(hc.Memory) // keep: ~70-field HostConfig+ hc.MemorySwap = mo.PointerToOption(req.HostConfig.MemorySwap).OrElse(hc.MemorySwap) // keep: ~70-field HostConfig+ hc.NanoCPUs = mo.PointerToOption(req.HostConfig.NanoCPUs).OrElse(hc.NanoCPUs) // keep: ~70-field HostConfig+ hc.CPUShares = mo.PointerToOption(req.HostConfig.CPUShares).OrElse(hc.CPUShares) // keep: ~70-field HostConfigreturn nil}-// buildEditNetworkingConfigInternal builds the networking config for an edit-// recreate. A nil request preserves the existing attachments (same behavior as-// redeploy); otherwise the request is the full desired endpoint set, keeping-// the sanitized settings of endpoints that stay attached. Containers using-// host/none/container: network modes never get endpoint edits.-func buildEditNetworkingConfigInternal(containerInspect container.InspectResponse, req *containertypes.NetworkingConfigCreate, apiVersion string) (*network.NetworkingConfig, error) {- if containerInspect.HostConfig != nil {- mode := containerInspect.HostConfig.NetworkMode- if mode.IsHost() || mode.IsNone() || mode.IsContainer() {- return nil, nil- }+// buildEditNetworkingConfig builds the networking config for an edit recreate. A nil request preserves the existing+// attachments like redeploy; otherwise it is the full desired endpoint set, keeping settings of endpoints that stay.+// Containers using host/none/container: network modes never get endpoint edits.+func buildEditNetworkingConfig(containerInspect container.InspectResponse, req *containertypes.NetworkingConfigCreate, apiVersion string) (*network.NetworkingConfig, error) {+ if mode := kit.FromPtr(containerInspect.HostConfig).NetworkMode; mode.IsHost() || mode.IsNone() || mode.IsContainer() {+ return nil, nil}if req == nil {- return buildCleanNetworkingConfigInternal(containerInspect, apiVersion), nil- }-- var existing map[string]*network.EndpointSettings- if containerInspect.NetworkSettings != nil {- existing = compat.SanitizeContainerCreateEndpointSettingsForDockerAPI(containerInspect.NetworkSettings.Networks, apiVersion)+ return buildCleanNetworkingConfig(containerInspect, apiVersion), nil}+ existing := kit.FromPtr(buildCleanNetworkingConfig(containerInspect, apiVersion)).EndpointsConfigendpoints := make(map[string]*network.EndpointSettings, len(req.EndpointsConfig))for name, epReq := range req.EndpointsConfig {- endpoint := &network.EndpointSettings{}- if prior := existing[name]; prior != nil {- priorCopy := *prior- priorCopy.IPAMConfig = nil- endpoint = &priorCopy- }- endpoint.Aliases = append([]string{}, epReq.Aliases...)- ipam, err := endpointIPAMFromCreateInternal(epReq)+ ipam, err := endpointIPAMFromCreate(epReq)if err != nil {return nil, err}- endpoint.IPAMConfig = ipam- endpoints[name] = endpoint+ prior := kit.FromPtr(existing[name])+ endpoints[name] = &network.EndpointSettings{ //exhaustruct:enforce+ IPAMConfig: ipam,+ Links: prior.Links,+ Aliases: append([]string{}, epReq.Aliases...),+ DriverOpts: prior.DriverOpts,+ GwPriority: prior.GwPriority,+ NetworkID: prior.NetworkID,+ EndpointID: prior.EndpointID,+ Gateway: prior.Gateway,+ IPAddress: prior.IPAddress,+ MacAddress: prior.MacAddress,+ IPPrefixLen: prior.IPPrefixLen,+ IPv6Gateway: prior.IPv6Gateway,+ GlobalIPv6Address: prior.GlobalIPv6Address,+ GlobalIPv6PrefixLen: prior.GlobalIPv6PrefixLen,+ DNSNames: prior.DNSNames,+ }}if len(endpoints) == 0 {@@ -1144,17 +970,12 @@ func (s *ContainerService) GetContainerEditConfig(ctx context.Context, containerreturn containertypes.EditConfig{}, fmt.Errorf("failed to inspect container: %w", err)}- containerInfo := containerJSON.Container- var containerLabels map[string]string- if containerInfo.Config != nil {- containerLabels = containerInfo.Config.Labels- }-- isCompose := docker.ComposeProjectLabel(containerLabels) != ""currentContainerID, currentContainerErr := cgroup.CurrentContainerID()- editDisabled := labels.ShouldDisableArcaneServerRedeploy(containerLabels, containerInfo.ID, currentContainerID, currentContainerErr)-- return containertypes.NewEditConfigFromInspect(&containerInfo, isCompose, editDisabled), nil+ return containertypes.NewEditConfigFromInspect(+ &containerJSON.Container,+ docker.ComposeProjectLabel(kit.FromPtr(containerJSON.Container.Config).Labels) != "",+ labels.ShouldDisableArcaneServerRedeploy(kit.FromPtr(containerJSON.Container.Config).Labels, containerJSON.Container.ID, currentContainerID, currentContainerErr),+ ), nil}// EditContainer applies a user-supplied config diff onto the container's@@ -1200,12 +1021,7 @@ func (s *ContainerService) EditContainer(ctx context.Context, containerID stringreturn "", err}- newName := containerName- if req.Name != nil {- if trimmed := strings.TrimPrefix(strings.TrimSpace(*req.Name), "/"); trimmed != "" {- newName = trimmed- }- }+ newName := cmp.Or(strings.TrimPrefix(strings.TrimSpace(kit.FromPtr(req.Name)), "/"), containerName)if newName != containerName {if _, inspectErr := compat.ContainerInspectWithCompatibility(ctx, dockerClient, newName, client.ContainerInspectOptions{}); inspectErr == nil {return "", fmt.Errorf("container name %s: %w", newName, common.ErrContainerNameTaken)@@ -1214,35 +1030,30 @@ func (s *ContainerService) EditContainer(ctx context.Context, containerID stringapiVersion := compat.DetectDockerAPIVersion(ctx, dockerClient)- newConfig := *containerInfo.Config- var newHostConfig container.HostConfig- if containerInfo.HostConfig != nil {- newHostConfig = *containerInfo.HostConfig- }-- if applyEditToContainerConfigErr := applyEditToContainerConfigInternal(&newConfig, newHostConfig.PortBindings, req); applyEditToContainerConfigErr != nil {- return "", common.Classify(common.ErrValidation, applyEditToContainerConfigErr)+ newHostConfig := kit.FromPtr(containerInfo.HostConfig)+ newConfig, err := applyEditToContainerConfig(*containerInfo.Config, newHostConfig.PortBindings, req)+ if err != nil {+ return "", common.Classify(common.ErrValidation, err)}- if applyEditToHostConfigErr := applyEditToHostConfigInternal(&newHostConfig, req); applyEditToHostConfigErr != nil {+ if applyEditToHostConfigErr := applyEditToHostConfig(&newHostConfig, req); applyEditToHostConfigErr != nil {return "", common.Classify(common.ErrValidation, applyEditToHostConfigErr)}- networkingConfig, err := buildEditNetworkingConfigInternal(containerInfo, req.NetworkingConfig, apiVersion)+ networkingConfig, err := buildEditNetworkingConfig(containerInfo, req.NetworkingConfig, apiVersion)if err != nil {return "", common.Classify(common.ErrValidation, err)}// Unlike redeploy, edit only pulls when the effective image is missing// locally so an unrelated config change never surprise-upgrades the image.- imageName := newConfig.Image- if imageName != "" {- if _, inspectErr := dockerClient.ImageInspect(ctx, imageName); inspectErr != nil {- if pullRedeployImageErr := s.pullRedeployImageInternal(ctx, dockerClient, imageName, containerID, containerName, "edit", req.Credentials, user); pullRedeployImageErr != nil {+ if newConfig.Image != "" {+ if _, inspectErr := dockerClient.ImageInspect(ctx, newConfig.Image); inspectErr != nil {+ if pullRedeployImageErr := s.pullRedeployImage(ctx, dockerClient, newConfig.Image, containerID, containerName, "edit", req.Credentials, user); pullRedeployImageErr != nil {return "", pullRedeployImageErr}}}- return s.recreateContainerInternal(ctx, dockerClient, containerInfo, "edit", newName, &newConfig, &newHostConfig, networkingConfig, apiVersion, event.EventTypeContainerUpdate, user)+ return s.recreateContainer(ctx, dockerClient, containerInfo, "edit", newName, &newConfig, &newHostConfig, networkingConfig, apiVersion, event.EventTypeContainerUpdate, user)}func (s *ContainerService) GetContainerByReference(ctx context.Context, ref string) (*container.InspectResponse, error) {@@ -1271,17 +1082,39 @@ func (s *ContainerService) GetContainerDetails(ctx context.Context, id string) (details := containertypes.NewDetails(containerInspect)currentContainerID, currentContainerErr := cgroup.CurrentContainerID()- details.RedeployDisabled = labels.ShouldDisableArcaneServerRedeploy(details.Labels, details.ID, currentContainerID, currentContainerErr)- var excluded map[string]bool- if s.settingsService != nil {- excluded = docker.ExcludedContainerNameSet(s.settingsService.GetStringSetting(ctx, "autoUpdateExcludedContainers", ""))+ updates, err := s.imageService.GetUpdateInfoByContainers(ctx, []container.Summary{{+ ID: details.ID,+ Image: details.Image,+ ImageID: details.ImageID,+ Labels: details.Labels,+ }})+ if err != nil {+ slog.WarnContext(ctx, "Failed to fetch container update info", "error", err)}- details.AutoUpdateEnabled = !labels.IsUpdateDisabled(details.Labels) && !docker.ContainerNameExcluded([]string{details.Name}, excluded)- updates := s.lookupContainerUpdateInfoInternal(ctx, []container.Summary{{ID: details.ID, Image: details.Image, ImageID: details.ImageID, Labels: details.Labels}})- details.UpdateInfo = updates[details.ID]- s.applyContainerDetailsIconInternal(ctx, &details)-- return details, nil+ resolvedIcon := s.resolveContainerIcon(ctx, details.Labels, map[string]projects.ArcaneComposeMetadata{})++ return containertypes.Details{ //exhaustruct:enforce+ ID: details.ID,+ Name: details.Name,+ Image: details.Image,+ ImageID: details.ImageID,+ Created: details.Created,+ State: details.State,+ Config: details.Config,+ HostConfig: details.HostConfig,+ NetworkSettings: details.NetworkSettings,+ Ports: details.Ports,+ Mounts: details.Mounts,+ Labels: details.Labels,+ ComposeInfo: details.ComposeInfo,+ IconLightURL: resolvedIcon.IconLightURL,+ IconDarkURL: resolvedIcon.IconDarkURL,+ RedeployDisabled: labels.ShouldDisableArcaneServerRedeploy(details.Labels, details.ID, currentContainerID, currentContainerErr),+ AutoUpdateEnabled: !labels.IsUpdateDisabled(details.Labels) &&+ !docker.ContainerNameExcluded([]string{details.Name}, docker.ExcludedContainerNameSet(s.settingsService.GetStringSetting(ctx, "autoUpdateExcludedContainers", ""))),+ UpdateInfo: updates[details.ID],+ ActivityID: details.ActivityID,+ }, nil}// containerProcessesPsArgs lists ps argument sets in preference order: procps first, then@@ -1298,11 +1131,7 @@ func (s *ContainerService) GetContainerProcesses(ctx context.Context, containerIreturn containertypes.Processes{}, fmt.Errorf("failed to connect to Docker: %w", err)}- timeout := timeouts.DefaultDockerAPI- if s.settingsService != nil {- timeout = timeouts.GetDuration(s.settingsService.GetSettingsConfig().DockerAPITimeout.AsInt(), timeouts.DefaultDockerAPI)- }- topCtx, cancel := context.WithTimeout(ctx, timeout)+ topCtx, cancel := context.WithTimeout(ctx, timeouts.GetDuration(s.settingsService.GetSettingsConfig().DockerAPITimeout.AsInt(), timeouts.DefaultDockerAPI))defer cancel()var result client.ContainerTopResult@@ -1313,16 +1142,23 @@ func (s *ContainerService) GetContainerProcesses(ctx context.Context, containerI}}if err != nil {- return containertypes.Processes{}, fmt.Errorf("failed to list container processes: %w", err)- }- processes := containertypes.Processes{Titles: result.Titles, Processes: result.Processes}- if processes.Titles == nil {- processes.Titles = []string{}- }- if processes.Processes == nil {- processes.Processes = [][]string{}+ return containertypes.Processes{}, common.Classify(func() error {+ switch {+ case errdefs.IsNotFound(err):+ return common.ErrNotFound+ case errdefs.IsConflict(err):+ return common.ErrConflict+ case errors.Is(err, context.DeadlineExceeded):+ return common.ErrTimeout+ default:+ return nil+ }+ }(), fmt.Errorf("failed to list container processes: %w", err))}- return processes, nil+ return containertypes.Processes{+ Titles: kit.Ternary(result.Titles == nil, []string{}, result.Titles),+ Processes: kit.Ternary(result.Processes == nil, [][]string{}, result.Processes),+ }, nil}// GetContainerNameByReference resolves a container's clean name from a Docker ID or name.@@ -1411,21 +1247,17 @@ func (s *ContainerService) DeleteContainer(ctx context.Context, containerID stri}// Remove named volumes if requested- if removeVolumes && len(volumesToRemove) > 0 {- for _, volumeName := range volumesToRemove {- if _, removeErr := dockerClient.VolumeRemove(ctx, volumeName, client.VolumeRemoveOptions{Force: false}); removeErr != nil {- // Log but don't fail if volume removal fails (might be in use by another container)- s.eventService.LogErrorEvent(ctx, event.EventTypeVolumeError, "volume", volumeName, "", user.ID, user.Username, "0", removeErr, database.JSON{"action": "delete", "container": containerID})- }+ for _, volumeName := range volumesToRemove {+ if _, removeErr := dockerClient.VolumeRemove(ctx, volumeName, client.VolumeRemoveOptions{Force: false}); removeErr != nil {+ // Log but don't fail if volume removal fails (might be in use by another container)+ s.eventService.LogErrorEvent(ctx, event.EventTypeVolumeError, "volume", volumeName, "", user.ID, user.Username, "0", removeErr, database.JSON{"action": "delete", "container": containerID})}}- metadata := database.JSON{+ err = s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDelete, containerID, "name", user.ID, user.Username, "0", database.JSON{"action": "delete","containerId": containerID,- }-- err = s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDelete, containerID, "name", user.ID, user.Username, "0", metadata)+ })if err != nil {return fmt.Errorf("failed to log action: %w", err)}@@ -1433,9 +1265,7 @@ func (s *ContainerService) DeleteContainer(ctx context.Context, containerID strireturn nil}-func (- s *ContainerService,-) CreateContainer(+func (s *ContainerService) CreateContainer(ctx context.Context,config *container.Config,hostConfig *container.HostConfig,@@ -1453,85 +1283,10 @@ func (return nil, fmt.Errorf("failed to connect to Docker: %w", err)}- _, err = dockerClient.ImageInspect(ctx, config.Image)- if err != nil {+ if _, inspectErr := dockerClient.ImageInspect(ctx, config.Image); inspectErr != nil {// Image not found locally, need to pull it- pullOptions, authErr := s.imageService.PullOptionsWithAuth(ctx, config.Image, credentials)- if authErr != nil {- slog.WarnContext(ctx, "Failed to get registry authentication for container image; proceeding without auth",- "image", config.Image,- "error", authErr.Error())- pullOptions = client.ImagePullOptions{}- }-- localSettings := s.settingsService.GetSettingsConfig()- pullCtx, pullCancel := context.WithTimeout(ctx, timeouts.GetDuration(localSettings.DockerImagePullTimeout.AsInt(), timeouts.DefaultDockerImagePull))- defer pullCancel()-- defer s.eventService.BeginDockerResourceSuppressionWindow("image", "", config.Image)()- reader, pullErr := dockerClient.ImagePull(pullCtx, config.Image, pullOptions)- if pullErr != nil {- if errors.Is(pullCtx.Err(), context.DeadlineExceeded) {- s.eventService.LogErrorEvent(- ctx,- event.EventTypeContainerError,- "container",- "",- containerName,- user.ID,- user.Username,- "0",- pullErr,- database.JSON{- "action": "create",- "image": config.Image,- "step": "pull_image_timeout",- },- )- return nil, fmt.Errorf("image pull timed out for %s (increase DOCKER_IMAGE_PULL_TIMEOUT or setting)", config.Image)- }- s.eventService.LogErrorEvent(- ctx,- event.EventTypeContainerError,- "container",- "",- containerName,- user.ID,- user.Username,- "0",- pullErr,- database.JSON{- "action": "create",- "image": config.Image,- "step": "pull_image",- },- )- return nil, fmt.Errorf("failed to pull image %s: %w", config.Image, pullErr)- }- defer func() { _ = reader.Close() }()-- progressWriter, _ := ctx.Value(types.ProgressWriterKey{}).(io.Writer)- logWriter := docker.NewLogLineWriter(progressWriter)- streamErr := docker.RenderJSONMessageStream(reader, logWriter)- _ = logWriter.Close()- if streamErr != nil {- s.eventService.LogErrorEvent(- ctx,- event.EventTypeContainerError,- "container",- "",- containerName,- user.ID,- user.Username,- "0",- streamErr,- database.JSON{- "action": "create",- "image": config.Image,- "step": "complete_pull",- },- )- return nil, fmt.Errorf("failed to complete image pull: %w", streamErr)+ if pullErr := s.pullRedeployImage(ctx, dockerClient, config.Image, "", containerName, "create", credentials, user); pullErr != nil {+ return nil, pullErr}}@@ -1563,12 +1318,10 @@ func (}defer s.eventService.BeginDockerResourceSuppressionWindow("container", resp.ID, containerName)()- metadata := database.JSON{+ if logErr := s.eventService.LogContainerEvent(ctx, event.EventTypeContainerCreate, resp.ID, "name", user.ID, user.Username, "0", database.JSON{"action": "create","containerId": resp.ID,- }-- if logErr := s.eventService.LogContainerEvent(ctx, event.EventTypeContainerCreate, resp.ID, "name", user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log container stop action", "error", logErr)}@@ -1670,7 +1423,11 @@ func (s *ContainerService) ListContainersPaginated(}dockerContainers = FilterExcludedContainers(dockerContainers, includeInternal, includeHidden)- updateInfoMap := s.lookupContainerUpdateInfoInternal(ctx, dockerContainers)+ // Update lookup failures degrade to missing status rather than a failed list.+ updateInfoMap, err := s.imageService.GetUpdateInfoByContainers(ctx, dockerContainers)+ if err != nil {+ slog.WarnContext(ctx, "Failed to fetch container update info", "error", err)+ }currentContainerID, currentContainerErr := cgroup.CurrentContainerID()items := s.BuildSummaries(ctx, dockerContainers, updateInfoMap, currentContainerID, currentContainerErr)@@ -1678,11 +1435,12 @@ func (s *ContainerService) ListContainersPaginated(counts := s.CalculateStatusCounts(items)if groupBy == containerGroupByProject {- ungroupedParams := params- ungroupedParams.Start = 0- ungroupedParams.Limit = -1-- result := config.SearchOrderAndPaginate(items, ungroupedParams)+ result := config.SearchOrderAndPaginate(items, pagination.QueryParams{+ SearchQuery: params.SearchQuery,+ SortParams: params.SortParams,+ Params: pagination.Params{Limit: -1, SkipCount: params.SkipCount},+ Filters: params.Filters,+ })groups, paginationResp := paginateContainerProjectGroups(result, params)// Icons must be resolved before flattening: groups hold value copies,@@ -1693,58 +1451,71 @@ func (s *ContainerService) ListContainersPaginated(}return ContainerListResult{- Items: flattenContainerProjectGroupsInternal(groups),+ Items: flattenContainerProjectGroups(groups),Groups: groups,Pagination: paginationResp,Counts: counts,}, nil}- if isContainerResourceSortRequestInternal(params, groupBy) {- return s.listContainersByResourceInternal(ctx, config, items, counts, params)- }-- result := config.SearchOrderAndPaginate(items, params)- s.ApplySummaryIcons(ctx, result.Items, nil)- paginationResp := pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params)-- return ContainerListResult{- Items: result.Items,- Pagination: paginationResp,- Counts: counts,- }, nil-}+ // Resource sorting is limited to the ungrouped list: project-grouped mode keeps the existing group pagination.+ if containertypes.IsResourceSort(params.Sort) {+ // Apply search and filters, collect stats for the matching containers, then sort and paginate.+ filtered := make([]containertypes.Summary, 0, len(items))+ for _, item := range items {+ if config.MatchesSearchAndFilters(item, params) {+ filtered = append(filtered, item)+ }+ }-// listContainersByResourceInternal applies search and filters, collects stats-// for the matching containers, then sorts and paginates.-func (s *ContainerService) listContainersByResourceInternal(- ctx context.Context,- config pagination.Config[containertypes.Summary],- items []containertypes.Summary,- counts containertypes.StatusCounts,- params pagination.QueryParams,-) (ContainerListResult, error) {- filtered := make([]containertypes.Summary, 0, len(items))- for _, item := range items {- if config.MatchesSearchAndFilters(item, params) {- filtered = append(filtered, item)+ samples, collectErr := s.stats.Collect(ctx, filtered, stats.ContainerResourceSampleTTL)+ if collectErr != nil {+ return ContainerListResult{}, collectErr+ }+ sampled := make([]containertypes.Summary, 0, len(filtered))+ for _, item := range filtered {+ sampled = append(sampled, containertypes.Summary{ //exhaustruct:enforce+ EnvironmentTag: item.EnvironmentTag,+ ID: item.ID,+ Names: item.Names,+ Image: item.Image,+ ImageID: item.ImageID,+ Command: item.Command,+ Created: item.Created,+ Ports: item.Ports,+ Labels: item.Labels,+ State: item.State,+ Status: item.Status,+ HostConfig: item.HostConfig,+ NetworkSettings: item.NetworkSettings,+ Mounts: item.Mounts,+ IconLightURL: item.IconLightURL,+ IconDarkURL: item.IconDarkURL,+ UpdateStrategy: item.UpdateStrategy,+ UpdateInfo: item.UpdateInfo,+ RedeployDisabled: item.RedeployDisabled,+ AutoUpdateEnabled: item.AutoUpdateEnabled,+ Hidden: item.Hidden,+ ResourceSample: samples[item.ID],+ })}- }- samples, err := s.stats.Collect(ctx, filtered, stats.ContainerResourceSampleTTL)- if err != nil {- return ContainerListResult{}, err- }- for i := range filtered {- filtered[i].ResourceSample = samples[filtered[i].ID]+ page := config.OrderAndPaginate(sampled, params)+ s.ApplySummaryIcons(ctx, page, nil)++ return ContainerListResult{+ Items: page,+ Pagination: pagination.BuildResponse(int64(len(sampled)), int64(len(items)), params),+ Counts: counts,+ }, nil}- page := config.OrderAndPaginate(filtered, params)- s.ApplySummaryIcons(ctx, page, nil)+ result := config.SearchOrderAndPaginate(items, params)+ s.ApplySummaryIcons(ctx, result.Items, nil)return ContainerListResult{- Items: page,- Pagination: pagination.BuildResponse(int64(len(filtered)), int64(len(items)), params),+ Items: result.Items,+ Pagination: pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params),Counts: counts,}, nil}@@ -1756,37 +1527,57 @@ func (s *ContainerService) AggregateContainers(ctx context.Context, params pagin}pages := make([]pagination.EnvironmentPage[containertypes.Summary], 0, len(collection.Results))- counts := containertypes.StatusCounts{}+ running, stopped, total := 0, 0, 0resourceSortSupported := truefor _, result := range collection.Results {- page := result.Valuepages = append(pages, pagination.EnvironmentPage[containertypes.Summary]{Environment: result.Environment,- Items: page.Data,- TotalAvailable: cmp.Or(page.Pagination.GrandTotalItems, page.Pagination.TotalItems),+ Items: result.Value.Data,+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),})- counts.RunningContainers += page.Counts.RunningContainers- counts.StoppedContainers += page.Counts.StoppedContainers- counts.TotalContainers += page.Counts.TotalContainers- resourceSortSupported = resourceSortSupported && page.ResourceSortSupported- }-- grouped := groupBy == containerGroupByProject- ungroupedParams := params- ungroupedParams.Start, ungroupedParams.Limit = 0, -1- data, resp := pagination.MergeEnvironmentPages(s.buildContainerPaginationConfig(), pages, kit.Ternary(grouped, ungroupedParams, params))- var groups []containertypes.SummaryGroup- if grouped {- groups, resp = paginateContainerProjectGroups(pagination.FilterResult[containertypes.Summary]{Items: data, TotalCount: resp.TotalItems, TotalAvailable: resp.GrandTotalItems}, params)- data = flattenContainerProjectGroupsInternal(groups)+ running += result.Value.Counts.RunningContainers+ stopped += result.Value.Counts.StoppedContainers+ total += result.Value.Counts.TotalContainers+ resourceSortSupported = resourceSortSupported && result.Value.ResourceSortSupported+ }++ if groupBy != containerGroupByProject {+ data, resp := pagination.MergeEnvironmentPages(s.buildContainerPaginationConfig(), pages, params)+ return ContainerPaginatedResponse{+ Success: true,+ Data: data,+ Counts: containertypes.StatusCounts{+ RunningContainers: running,+ StoppedContainers: stopped,+ TotalContainers: total,+ },+ Pagination: handlerutil.PaginationResponse(resp),+ ResourceSortSupported: resourceSortSupported,+ FailedEnvironments: collection.Failures,+ }, nil}+ data, resp := pagination.MergeEnvironmentPages(s.buildContainerPaginationConfig(), pages, pagination.QueryParams{+ SearchQuery: params.SearchQuery,+ SortParams: params.SortParams,+ Params: pagination.Params{Limit: -1, SkipCount: params.SkipCount},+ Filters: params.Filters,+ })+ groups, groupedResp := paginateContainerProjectGroups(pagination.FilterResult[containertypes.Summary]{+ Items: data,+ TotalCount: resp.TotalItems,+ TotalAvailable: resp.GrandTotalItems,+ }, params)return ContainerPaginatedResponse{- Success: true,- Data: data,- Groups: groups,- Counts: counts,- Pagination: handlerutil.PaginationResponse(resp),+ Success: true,+ Data: flattenContainerProjectGroups(groups),+ Groups: groups,+ Counts: containertypes.StatusCounts{+ RunningContainers: running,+ StoppedContainers: stopped,+ TotalContainers: total,+ },+ Pagination: handlerutil.PaginationResponse(groupedResp),ResourceSortSupported: resourceSortSupported,FailedEnvironments: collection.Failures,}, nil@@ -1847,25 +1638,24 @@ func paginateContainerProjectGroups(}func groupContainersByProject(items []containertypes.Summary) []containertypes.SummaryGroup {- groups := make([]containertypes.SummaryGroup, 0)- groupIndexes := make(map[string]int)-+ groupNames := make([]string, 0)+ itemsByGroup := make(map[string][]containertypes.Summary)for _, item := range items {- groupName := getContainerProjectNameInternal(item)- groupIndex, exists := groupIndexes[groupName]- if !exists {- groupIndex = len(groups)- groupIndexes[groupName] = groupIndex- groups = append(groups, containertypes.SummaryGroup{GroupName: groupName})+ groupName := cmp.Or(docker.ComposeProjectLabel(item.Labels), containerNoProjectGroup)+ if _, exists := itemsByGroup[groupName]; !exists {+ groupNames = append(groupNames, groupName)}-- groups[groupIndex].Items = append(groups[groupIndex].Items, item)+ itemsByGroup[groupName] = append(itemsByGroup[groupName], item)}+ groups := make([]containertypes.SummaryGroup, 0, len(groupNames))+ for _, groupName := range groupNames {+ groups = append(groups, containertypes.SummaryGroup{GroupName: groupName, Items: itemsByGroup[groupName]})+ }return groups}-func flattenContainerProjectGroupsInternal(groups []containertypes.SummaryGroup) []containertypes.Summary {+func flattenContainerProjectGroups(groups []containertypes.SummaryGroup) []containertypes.Summary {flattened := make([]containertypes.Summary, 0)for _, group := range groups {flattened = append(flattened, group.Items...)@@ -1874,15 +1664,6 @@ func flattenContainerProjectGroupsInternal(groups []containertypes.SummaryGroup)return flattened}-func getContainerProjectNameInternal(localContainer containertypes.Summary) string {- if localContainer.Labels == nil {- return containerNoProjectGroup- }-- projectName := docker.ComposeProjectLabel(localContainer.Labels)- return kit.Ternary(projectName == "", containerNoProjectGroup, projectName)-}-// FilterExcludedContainers removes internal and hidden containers unless their corresponding inclusion flags are enabled.func FilterExcludedContainers(containers []container.Summary, includeInternal, includeHidden bool) []container.Summary {if includeInternal && includeHidden {@@ -1901,23 +1682,7 @@ func FilterExcludedContainers(containers []container.Summary, includeInternal, ireturn filtered}-// lookupContainerUpdateInfoInternal resolves stored update checks keyed by-// container ID; lookup failures degrade to missing status rather than a failed list.-func (s *ContainerService) lookupContainerUpdateInfoInternal(ctx context.Context, containers []container.Summary) map[string]*imagetypes.UpdateInfo {- if s.imageService == nil || len(containers) == 0 {- return map[string]*imagetypes.UpdateInfo{}- }- updates, err := s.imageService.GetUpdateInfoByContainers(ctx, containers)- if err != nil {- slog.WarnContext(ctx, "Failed to fetch container update info", "error", err)- return map[string]*imagetypes.UpdateInfo{}- }- return updates-}--func (- s *ContainerService,-) BuildSummaries(+func (s *ContainerService) BuildSummaries(ctx context.Context,containers []container.Summary,updateInfoMap map[string]*imagetypes.UpdateInfo,@@ -1925,23 +1690,36 @@ func (currentContainerErr error,) []containertypes.Summary {items := make([]containertypes.Summary, 0, len(containers))- var excluded map[string]bool- if s.settingsService != nil {- excluded = docker.ExcludedContainerNameSet(s.settingsService.GetStringSetting(ctx, "autoUpdateExcludedContainers", ""))- }+ excluded := docker.ExcludedContainerNameSet(s.settingsService.GetStringSetting(ctx, "autoUpdateExcludedContainers", ""))for _, dc := range containers {summary := containertypes.NewSummary(dc)policy := updater.DefaultLabelPolicy().TagPolicy(dc.Labels)resolved, policyErr := tagpolicy.Resolve(dc.Image, policy)- summary.UpdateStrategy = resolved.Strategy- if policyErr != nil {- summary.UpdateStrategy = cmp.Or(policy.Strategy, "auto")- }- summary.UpdateInfo = updateInfoMap[dc.ID]- summary.RedeployDisabled = labels.ShouldDisableArcaneServerRedeploy(summary.Labels, summary.ID, currentContainerID, currentContainerErr)- summary.AutoUpdateEnabled = !labels.IsUpdateDisabled(dc.Labels) && !docker.ContainerNameExcluded(dc.Names, excluded)- summary.Hidden, _ = kit.ParseBool(dc.Labels[libarcane.HiddenResourceLabel])- items = append(items, summary)+ hidden, _ := kit.ParseBool(dc.Labels[libarcane.HiddenResourceLabel])+ items = append(items, containertypes.Summary{ //exhaustruct:enforce+ EnvironmentTag: summary.EnvironmentTag,+ ID: summary.ID,+ Names: summary.Names,+ Image: summary.Image,+ ImageID: summary.ImageID,+ Command: summary.Command,+ Created: summary.Created,+ Ports: summary.Ports,+ Labels: summary.Labels,+ State: summary.State,+ Status: summary.Status,+ HostConfig: summary.HostConfig,+ NetworkSettings: summary.NetworkSettings,+ Mounts: summary.Mounts,+ IconLightURL: summary.IconLightURL,+ IconDarkURL: summary.IconDarkURL,+ UpdateStrategy: kit.Ternary(policyErr != nil, cmp.Or(policy.Strategy, "auto"), resolved.Strategy),+ UpdateInfo: updateInfoMap[dc.ID],+ RedeployDisabled: labels.ShouldDisableArcaneServerRedeploy(summary.Labels, summary.ID, currentContainerID, currentContainerErr),+ AutoUpdateEnabled: !labels.IsUpdateDisabled(dc.Labels) && !docker.ContainerNameExcluded(dc.Names, excluded),+ Hidden: hidden,+ ResourceSample: summary.ResourceSample,+ })}return items}@@ -1954,80 +1732,61 @@ func (s *ContainerService) ApplySummaryIcons(ctx context.Context, summaries []cometadataByProject = map[string]projects.ArcaneComposeMetadata{}}for i := range summaries {- s.applyContainerSummaryIconInternal(ctx, &summaries[i], metadataByProject)+ resolvedIcon := s.resolveContainerIcon(ctx, summaries[i].Labels, metadataByProject)+ summaries[i].IconLightURL = resolvedIcon.IconLightURL // keep: the exported API enriches caller-owned rows in place+ summaries[i].IconDarkURL = resolvedIcon.IconDarkURL // keep: the exported API enriches caller-owned rows in place}}-func (s *ContainerService) applyContainerSummaryIconInternal(ctx context.Context, summary *containertypes.Summary, metadataByProject map[string]projects.ArcaneComposeMetadata) {- if summary == nil {- return- }- resolvedIcon := s.resolveContainerIconInternal(ctx, summary.Labels, metadataByProject)- summary.IconLightURL = resolvedIcon.IconLightURL- summary.IconDarkURL = resolvedIcon.IconDarkURL-}--func (s *ContainerService) applyContainerDetailsIconInternal(ctx context.Context, details *containertypes.Details) {- if details == nil {- return- }- resolvedIcon := s.resolveContainerIconInternal(ctx, details.Labels, nil)- details.IconLightURL = resolvedIcon.IconLightURL- details.IconDarkURL = resolvedIcon.IconDarkURL-}--func (s *ContainerService) resolveContainerIconInternal(ctx context.Context, localLabels map[string]string, metadataByProject map[string]projects.ArcaneComposeMetadata) iconcatalog.ResolvedIconSet {+func (s *ContainerService) resolveContainerIcon(ctx context.Context, localLabels map[string]string, metadataByProject map[string]projects.ArcaneComposeMetadata) iconcatalog.ResolvedIconSet {explicitIcon := projects.FindArcaneIconSet(localLabels)- if !explicitIcon.IsEmpty() {- return iconcatalog.Resolve(project.IconCatalogForContext(ctx), explicitIcon)- }-projectName := docker.ComposeProjectLabel(localLabels)- if projectName == "" || s == nil || s.projectService == nil {+ if !explicitIcon.IsEmpty() || projectName == "" {return iconcatalog.Resolve(project.IconCatalogForContext(ctx), explicitIcon)}- meta := s.getCachedProjectIconMetadataInternal(ctx, projectName, metadataByProject)+ meta, ok := metadataByProject[projectName]+ if !ok {+ meta, ok, _ = s.iconMetaCache.Get(projectName)+ }+ if !ok {+ meta = func() projects.ArcaneComposeMetadata {+ proj, err := s.projectService.GetProjectByComposeName(ctx, projectName)+ if err != nil {+ return projects.ArcaneComposeMetadata{ServiceIconSets: map[string]projects.IconSet{}}+ }+ return s.projectService.ProjectMetadata(ctx, *proj, nil)+ }()+ s.iconMetaCache.Set(projectName, meta)+ }+ metadataByProject[projectName] = meta- serviceName := docker.ComposeServiceLabel(localLabels)return iconcatalog.Resolve(project.IconCatalogForContext(ctx), cmp.Or(explicitIcon,- meta.ServiceIconSets[serviceName],+ meta.ServiceIconSets[docker.ComposeServiceLabel(localLabels)],meta.ProjectIcon,))}-func (s *ContainerService) getCachedProjectIconMetadataInternal(ctx context.Context, projectName string, metadataByProject map[string]projects.ArcaneComposeMetadata) projects.ArcaneComposeMetadata {- if metadataByProject != nil {- if meta, ok := metadataByProject[projectName]; ok {- return meta- }- }-- if s.iconMetaCache != nil {- if meta, ok, _ := s.iconMetaCache.Get(projectName); ok {- if metadataByProject != nil {- metadataByProject[projectName] = meta+func (s *ContainerService) buildContainerPaginationConfig() pagination.Config[containertypes.Summary] {+ comparePorts := func(descending bool) pagination.SortOption[containertypes.Summary] {+ return func(a, b containertypes.Summary) int {+ hasPortsA, portA := lowestContainerPortSortValue(a.Ports)+ hasPortsB, portB := lowestContainerPortSortValue(b.Ports)++ switch {+ case !hasPortsA && !hasPortsB, portA == portB:+ return stats.CompareContainerNamesForSort(a, b)+ case !hasPortsA:+ return 1+ case !hasPortsB:+ return -1+ default:+ return kit.Ternary((portA < portB) != descending, -1, 1)}- return meta}}- meta := projects.ArcaneComposeMetadata{ServiceIconSets: map[string]projects.IconSet{}}- proj, err := s.projectService.GetProjectByComposeName(ctx, projectName)- if err == nil && proj != nil {- meta = s.projectService.ProjectMetadata(ctx, *proj, nil)- }- if s.iconMetaCache != nil {- s.iconMetaCache.Set(projectName, meta)- }- if metadataByProject != nil {- metadataByProject[projectName] = meta- }- return meta-}--func (s *ContainerService) buildContainerPaginationConfig() pagination.Config[containertypes.Summary] {return pagination.Config[containertypes.Summary]{SearchAccessors: []pagination.SearchAccessor[containertypes.Summary]{func(c containertypes.Summary) (string, error) {@@ -2040,122 +1799,93 @@ func (s *ContainerService) buildContainerPaginationConfig() pagination.Config[cofunc(c containertypes.Summary) (string, error) { return c.State, nil },func(c containertypes.Summary) (string, error) { return c.Status, nil },},- SortBindings: s.buildContainerSortBindings(),- FilterAccessors: s.buildContainerFilterAccessors(),- }-}--func (s *ContainerService) buildContainerSortBindings() []pagination.SortBinding[containertypes.Summary] {- return []pagination.SortBinding[containertypes.Summary]{- {- Key: "name",- Fn: func(a, b containertypes.Summary) int {- nameA, nameB := "", ""- if len(a.Names) > 0 {- nameA = a.Names[0]- }- if len(b.Names) > 0 {- nameB = b.Names[0]- }- return strings.Compare(nameA, nameB)+ SortBindings: []pagination.SortBinding[containertypes.Summary]{+ {+ Key: "name",+ Fn: stats.CompareContainerNamesForSort,},- },- {- Key: "image",- Fn: func(a, b containertypes.Summary) int {- return strings.Compare(a.Image, b.Image)+ {+ Key: "image",+ Fn: func(a, b containertypes.Summary) int {+ return strings.Compare(a.Image, b.Image)+ },},- },- {- Key: "state",- Fn: func(a, b containertypes.Summary) int {- return strings.Compare(a.State, b.State)+ {+ Key: "state",+ Fn: func(a, b containertypes.Summary) int {+ return strings.Compare(a.State, b.State)+ },},- },- {- Key: "status",- Fn: func(a, b containertypes.Summary) int {- return strings.Compare(a.Status, b.Status)+ {+ Key: "status",+ Fn: func(a, b containertypes.Summary) int {+ return strings.Compare(a.Status, b.Status)+ },},- },- {- Key: "ports",- Fn: compareContainerPortsForSortInternal,- DescFn: compareContainerPortsForSortDescInternal,- },- {- Key: "created",- Fn: func(a, b containertypes.Summary) int {- if a.Created < b.Created {- return -1- }- return kit.Ternary(a.Created > b.Created, 1, 0)+ {+ Key: "ports",+ Fn: comparePorts(false),+ DescFn: comparePorts(true),+ },+ {+ Key: "created",+ Fn: func(a, b containertypes.Summary) int {+ return cmp.Compare(a.Created, b.Created)+ },+ },+ {+ Key: containertypes.SortCPUUsage,+ Fn: stats.ContainerResourceSampleSort(containertypes.SortCPUUsage, false),+ DescFn: stats.ContainerResourceSampleSort(containertypes.SortCPUUsage, true),+ },+ {+ Key: containertypes.SortMemoryUsage,+ Fn: stats.ContainerResourceSampleSort(containertypes.SortMemoryUsage, false),+ DescFn: stats.ContainerResourceSampleSort(containertypes.SortMemoryUsage, true),},},- {- Key: containertypes.SortCPUUsage,- Fn: stats.ContainerResourceSampleSort(containertypes.SortCPUUsage, false),- DescFn: stats.ContainerResourceSampleSort(containertypes.SortCPUUsage, true),- },- {- Key: containertypes.SortMemoryUsage,- Fn: stats.ContainerResourceSampleSort(containertypes.SortMemoryUsage, false),- DescFn: stats.ContainerResourceSampleSort(containertypes.SortMemoryUsage, true),+ FilterAccessors: []pagination.FilterAccessor[containertypes.Summary]{+ {+ Key: "updates",+ Fn: func(c containertypes.Summary, filterValue string) bool {+ switch filterValue {+ case "has_update":+ return kit.FromPtr(c.UpdateInfo).HasUpdate+ case "up_to_date":+ return c.UpdateInfo != nil && !c.UpdateInfo.HasUpdate && c.UpdateInfo.Error == ""+ case "error":+ return kit.FromPtr(c.UpdateInfo).Error != ""+ case "unknown":+ return c.UpdateInfo == nil+ default:+ return true+ }+ },+ },+ {+ Key: "standalone",+ Fn: func(c containertypes.Summary, filterValue string) bool {+ value, valid := kit.ParseBool(filterValue)+ return !valid || (docker.ComposeProjectLabel(c.Labels) == "" && !labels.IsSwarmTask(c.Labels)) == value+ },+ },+ {+ Key: "label",+ NoSplit: true,+ Fn: func(c containertypes.Summary, filterValue string) bool {+ key, value, hasValue := strings.Cut(filterValue, "=")+ if strings.TrimSpace(key) == "" {+ return true+ }+ actual, ok := c.Labels[strings.TrimSpace(key)]+ return ok && (!hasValue || actual == value)+ },+ },},}}-// isContainerResourceSortRequestInternal limits resource sorting to the-// ungrouped list: project-grouped mode keeps the existing group pagination.-func isContainerResourceSortRequestInternal(params pagination.QueryParams, groupBy string) bool {- return groupBy != containerGroupByProject && containertypes.IsResourceSort(params.Sort)-}--func compareContainerPortsForSortInternal(a, b containertypes.Summary) int {- hasPortsA, portA := lowestContainerPortSortValueInternal(a.Ports)- hasPortsB, portB := lowestContainerPortSortValueInternal(b.Ports)-- switch {- case !hasPortsA && !hasPortsB:- return stats.CompareContainerNamesForSort(a, b)- case !hasPortsA:- return 1- case !hasPortsB:- return -1- case portA < portB:- return -1- case portA > portB:- return 1- default:- return stats.CompareContainerNamesForSort(a, b)- }-}--func compareContainerPortsForSortDescInternal(a, b containertypes.Summary) int {- hasPortsA, portA := lowestContainerPortSortValueInternal(a.Ports)- hasPortsB, portB := lowestContainerPortSortValueInternal(b.Ports)-- switch {- case !hasPortsA && !hasPortsB:- return stats.CompareContainerNamesForSort(a, b)- case !hasPortsA:- return 1- case !hasPortsB:- return -1- case portA > portB:- return -1- case portA < portB:- return 1- default:- return stats.CompareContainerNamesForSort(a, b)- }-}--func lowestContainerPortSortValueInternal(ports []containertypes.Port) (bool, int) {- if len(ports) == 0 {- return false, 0- }-+func lowestContainerPortSortValue(ports []containertypes.Port) (bool, int) {lowestPublished := 0lowestPrivate := 0for _, port := range ports {@@ -2167,71 +1897,22 @@ func lowestContainerPortSortValueInternal(ports []containertypes.Port) (bool, in}}- switch {- case lowestPublished > 0:- return true, lowestPublished- case lowestPrivate > 0:- return true, lowestPrivate- default:- return false, 0- }-}--func (s *ContainerService) buildContainerFilterAccessors() []pagination.FilterAccessor[containertypes.Summary] {- return []pagination.FilterAccessor[containertypes.Summary]{- {- Key: "updates",- Fn: func(c containertypes.Summary, filterValue string) bool {- switch filterValue {- case "has_update":- return c.UpdateInfo != nil && c.UpdateInfo.HasUpdate- case "up_to_date":- return c.UpdateInfo != nil && !c.UpdateInfo.HasUpdate && c.UpdateInfo.Error == ""- case "error":- return c.UpdateInfo != nil && c.UpdateInfo.Error != ""- case "unknown":- return c.UpdateInfo == nil- default:- return true- }- },- },- {- Key: "standalone",- Fn: func(c containertypes.Summary, filterValue string) bool {- isStandalone := docker.ComposeProjectLabel(c.Labels) == "" && !labels.IsSwarmTask(c.Labels)- value, valid := kit.ParseBool(filterValue)- return !valid || isStandalone == value- },- },- {- Key: "label",- NoSplit: true,- Fn: func(c containertypes.Summary, filterValue string) bool {- key, value, hasValue := strings.Cut(filterValue, "=")- key = strings.TrimSpace(key)- if key == "" {- return true- }- actual, ok := c.Labels[key]- return ok && (!hasValue || actual == value)- },- },- }+ lowest := cmp.Or(lowestPublished, lowestPrivate)+ return lowest > 0, lowest}func (s *ContainerService) CalculateStatusCounts(items []containertypes.Summary) containertypes.StatusCounts {- counts := containertypes.StatusCounts{- TotalContainers: len(items),- }+ running := 0for _, c := range items {if c.State == "running" {- counts.RunningContainers++- } else {- counts.StoppedContainers+++ running++}}- return counts+ return containertypes.StatusCounts{+ RunningContainers: running,+ StoppedContainers: len(items) - running,+ TotalContainers: len(items),+ }}// CreateExec creates an exec instance in the container@@ -2241,15 +1922,13 @@ func (s *ContainerService) CreateExec(ctx context.Context, containerID string, creturn "", fmt.Errorf("failed to connect to Docker: %w", err)}- execConfig := client.ExecCreateOptions{+ execResp, err := dockerClient.ExecCreate(ctx, containerID, client.ExecCreateOptions{AttachStdin: true,AttachStdout: true,AttachStderr: true,TTY: true,Cmd: cmd,- }-- execResp, err := dockerClient.ExecCreate(ctx, containerID, execConfig)+ })if err != nil {return "", fmt.Errorf("failed to create exec: %w", err)}@@ -2257,15 +1936,6 @@ func (s *ContainerService) CreateExec(ctx context.Context, containerID string, creturn execResp.ID, nil}-// ExecSession manages the lifecycle of a Docker exec session.-type ExecSession struct {- execID string- containerID string- hijackedResp client.HijackedResponse- dockerClient *client.Client- closeOnce sync.Once-}-func (e *ExecSession) Stdin() io.WriteCloser { return e.hijackedResp.Conn }func (e *ExecSession) Stdout() io.Reader { return e.hijackedResp.Reader }@@ -2310,18 +1980,11 @@ func (s *ContainerService) AttachExec(ctx context.Context, containerID, execID s}, nil}-type demuxedLogsInternal struct {- io.Reader-- pipe *io.PipeReader- logs io.ReadCloser-}--func (d *demuxedLogsInternal) Close() error {+func (d *demuxedLogs) Close() error {return errors.Join(d.logs.Close(), d.pipe.Close())}-func (s *ContainerService) openLogsInternal(ctx context.Context, containerID string, options client.ContainerLogsOptions) (io.ReadCloser, client.ContainerInspectResult, error) {+func (s *ContainerService) openLogs(ctx context.Context, containerID string, options client.ContainerLogsOptions) (io.ReadCloser, client.ContainerInspectResult, error) {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return nil, client.ContainerInspectResult{}, fmt.Errorf("failed to connect to Docker: %w", err)@@ -2340,7 +2003,7 @@ func (s *ContainerService) openLogsInternal(ctx context.Context, containerID str}func (s *ContainerService) StreamLogs(ctx context.Context, containerID string, logsChan chan<- string, follow bool, tail, since string, timestamps bool) error {- logs, containerInspect, err := s.openLogsInternal(ctx, containerID, client.ContainerLogsOptions{+ logs, containerInspect, err := s.openLogs(ctx, containerID, client.ContainerLogsOptions{ShowStdout: true,ShowStderr: true,Follow: follow,@@ -2353,14 +2016,13 @@ func (s *ContainerService) StreamLogs(ctx context.Context, containerID string, l}defer func() { _ = logs.Close() }()- isTTY := containerInspect.Container.Config != nil && containerInspect.Container.Config.Tty- return docker.StreamContainerLogs(ctx, logs, logsChan, follow, isTTY)+ return docker.StreamContainerLogs(ctx, logs, logsChan, follow, kit.FromPtr(containerInspect.Container.Config).Tty)}// DownloadLogs returns every log line Docker retains for the container as a// single plain-text stream plus the attachment filename. Callers must Close it.func (s *ContainerService) DownloadLogs(ctx context.Context, containerID string) (io.ReadCloser, string, error) {- logs, containerInspect, err := s.openLogsInternal(ctx, containerID, client.ContainerLogsOptions{+ logs, containerInspect, err := s.openLogs(ctx, containerID, client.ContainerLogsOptions{ShowStdout: true,ShowStderr: true,Tail: "all",@@ -2371,7 +2033,7 @@ func (s *ContainerService) DownloadLogs(ctx context.Context, containerID string)}filename := "container-" + containerInspect.Container.ID[:min(12, len(containerInspect.Container.ID))] + "-logs.log"- if containerInspect.Container.Config != nil && containerInspect.Container.Config.Tty {+ if kit.FromPtr(containerInspect.Container.Config).Tty {return logs, filename, nil}@@ -2383,5 +2045,9 @@ func (s *ContainerService) DownloadLogs(ctx context.Context, containerID string)}_ = pw.CloseWithError(copyErr)}()- return &demuxedLogsInternal{Reader: pr, pipe: pr, logs: logs}, filename, nil+ return &demuxedLogs{+ Reader: pr,+ pipe: pr,+ logs: logs,+ }, filename, nil}diff --git a/backend/internal/container/service_test.go b/backend/internal/container/service_test.goindex 048dc08fe..3d9c90011 100644--- a/backend/internal/container/service_test.go+++ b/backend/internal/container/service_test.go@@ -39,6 +39,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/getarcaneapp/arcane/backend/v2/internal/docker""github.com/getarcaneapp/arcane/backend/v2/internal/event"+ imagedomain "github.com/getarcaneapp/arcane/backend/v2/internal/image""github.com/getarcaneapp/arcane/backend/v2/internal/imageupdate""github.com/getarcaneapp/arcane/backend/v2/internal/project""github.com/getarcaneapp/arcane/backend/v2/internal/settings"@@ -47,6 +48,19 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx")+type resourceStatsServer struct {+ mu sync.Mutex+ containers []container.Summary+ stats map[string]container.StatsResponse+ statsStatus map[string]int+ statsCalls map[string]int+ delay time.Duration+ slowIDs map[string]bool++ current atomic.Int32+ maxSeen atomic.Int32+}+func TestPaginateContainerProjectGroupsKeepsProjectWhole(t *testing.T) {items := []containertypes.Summary{newGroupedContainerSummary("other-1", "other-1"),@@ -127,7 +141,10 @@ func TestPaginateContainerProjectGroupsSelectsRequestedPage(t *testing.T) {}func TestContainerSummaryIconsAppliedAfterGrouping(t *testing.T) {- service := &ContainerService{}+ db := setupProjectTestDB(t)+ settingsService, err := settings.NewSettingsService(t.Context(), db)+ require.NoError(t, err)+ service := NewContainerService(nil, nil, nil, settingsService, project.NewProjectService(db, settingsService, nil, nil, nil, nil, nil, nil, nil, nil, nil))dockerContainers := []container.Summary{{ID: "app", Names: []string{"/app"}, Labels: map[string]string{"com.docker.compose.project": "media",@@ -151,7 +168,7 @@ func TestContainerSummaryIconsAppliedAfterGrouping(t *testing.T) {for gi := range groups {service.ApplySummaryIcons(t.Context(), groups[gi].Items, map[string]projects.ArcaneComposeMetadata{})}- flattened := flattenContainerProjectGroupsInternal(groups)+ flattened := flattenContainerProjectGroups(groups)require.Len(t, groups, 1)require.NotEmpty(t, groups[0].Items[0].IconLightURL)@@ -181,8 +198,6 @@ func TestGroupContainersByProjectUsesNoProjectBucket(t *testing.T) {require.Len(t, groups[0].Items, 1)require.Equal(t, containerNoProjectGroup, groups[1].GroupName)require.Len(t, groups[1].Items, 2)- require.Equal(t, containerNoProjectGroup, getContainerProjectNameInternal(groups[1].Items[0]))- require.Equal(t, containerNoProjectGroup, getContainerProjectNameInternal(groups[1].Items[1]))}func TestBuildContainerFilterAccessors_FiltersStandaloneContainers(t *testing.T) {@@ -194,7 +209,7 @@ func TestBuildContainerFilterAccessors_FiltersStandaloneContainers(t *testing.T){ID: "swarm-task", Labels: map[string]string{"com.docker.swarm.service.name": "web"}, UpdateInfo: updateInfo},}- result := pagination.Config[containertypes.Summary]{FilterAccessors: service.buildContainerFilterAccessors()}.SearchOrderAndPaginate(+ result := pagination.Config[containertypes.Summary]{FilterAccessors: service.buildContainerPaginationConfig().FilterAccessors}.SearchOrderAndPaginate(items,pagination.QueryParams{Filters: map[string]string{"standalone": "true", "updates": "has_update"}},)@@ -211,7 +226,7 @@ func TestBuildContainerFilterAccessors_FiltersByLabel(t *testing.T) {{ID: "other", Labels: map[string]string{"heal": "false"}},{ID: "bare", Labels: map[string]string{}},}- config := pagination.Config[containertypes.Summary]{FilterAccessors: service.buildContainerFilterAccessors()}+ config := pagination.Config[containertypes.Summary]{FilterAccessors: service.buildContainerPaginationConfig().FilterAccessors}tests := []struct {name string@@ -236,7 +251,7 @@ func TestBuildContainerFilterAccessors_FiltersByLabel(t *testing.T) {}}-func TestBuildCleanNetworkingConfigInternalPreservesEndpointSettings(t *testing.T) {+func TestBuildCleanNetworkingConfigPreservesEndpointSettings(t *testing.T) {containerInspect := container.InspectResponse{NetworkSettings: &container.NetworkSettings{Networks: map[string]*network.EndpointSettings{@@ -249,7 +264,7 @@ func TestBuildCleanNetworkingConfigInternalPreservesEndpointSettings(t *testing.},}- out := buildCleanNetworkingConfigInternal(containerInspect, "1.44")+ out := buildCleanNetworkingConfig(containerInspect, "1.44")require.NotNil(t, out)require.Contains(t, out.EndpointsConfig, "bridge")require.Equal(t, []string{"svc"}, out.EndpointsConfig["bridge"].Aliases)@@ -273,16 +288,21 @@ func TestCompareContainerPortsForSortDesc_KeepsContainersWithoutPortsLast(t *tesNames: []string{"/none"},}- require.Equal(t, -1, compareContainerPortsForSortDescInternal(withPublished, withPrivateOnly))- require.Equal(t, -1, compareContainerPortsForSortDescInternal(withPrivateOnly, withoutPorts))- require.Equal(t, 1, compareContainerPortsForSortDescInternal(withoutPorts, withPublished))+ sortBindings := (&ContainerService{}).buildContainerPaginationConfig().SortBindings+ comparePortsDesc := sortBindings[slices.IndexFunc(sortBindings, func(binding pagination.SortBinding[containertypes.Summary]) bool {+ return binding.Key == "ports"+ })].DescFn++ require.Equal(t, -1, comparePortsDesc(withPublished, withPrivateOnly))+ require.Equal(t, -1, comparePortsDesc(withPrivateOnly, withoutPorts))+ require.Equal(t, 1, comparePortsDesc(withoutPorts, withPublished))}-func TestContainerServiceCommitContainerCallsDockerAPIInternal(t *testing.T) {- db := setupProjectTestDBInternal(t)+func TestContainerServiceCommitContainerCallsDockerAPI(t *testing.T) {+ db := setupProjectTestDB(t)var gotRequest map[string]anyserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodPost || dockerTestPathInternal(r.URL.Path) != "/commit" {+ if r.Method != http.MethodPost || dockerTestPath(r.URL.Path) != "/commit" {http.NotFound(w, r)return}@@ -299,7 +319,7 @@ func TestContainerServiceCommitContainerCallsDockerAPIInternal(t *testing.T) {svc := NewContainerService(event.NewEventService(db, nil, nil),- docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)),+ docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)),nil,nil,nil,@@ -327,11 +347,11 @@ func TestContainerServiceCommitContainerCallsDockerAPIInternal(t *testing.T) {require.Equal(t, "container-1", *evt.ResourceID)}-func TestContainerServiceCommitContainerOmitsReferenceWhenRepositoryEmptyInternal(t *testing.T) {- db := setupProjectTestDBInternal(t)+func TestContainerServiceCommitContainerOmitsReferenceWhenRepositoryEmpty(t *testing.T) {+ db := setupProjectTestDB(t)var gotRequest map[string]anyserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodPost || dockerTestPathInternal(r.URL.Path) != "/commit" {+ if r.Method != http.MethodPost || dockerTestPath(r.URL.Path) != "/commit" {http.NotFound(w, r)return}@@ -346,7 +366,7 @@ func TestContainerServiceCommitContainerOmitsReferenceWhenRepositoryEmptyInternasvc := NewContainerService(event.NewEventService(db, nil, nil),- docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)),+ docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)),nil,nil,nil,@@ -377,14 +397,14 @@ func newGroupedContainerSummary(name, localProject string) containertypes.Summar}}-// dockerTestPathInternal strips the /v1.NN version prefix Docker clients prepend, so+// dockerTestPath strips the /v1.NN version prefix Docker clients prepend, so// fake Docker servers can match on the bare path.-func dockerTestPathInternal(path string) string {- return dockerAPIVersionPrefixInternal.ReplaceAllString(path, "")+func dockerTestPath(path string) string {+ return dockerAPIVersionPrefix.ReplaceAllString(path, "")}-// newTestDockerClientInternal builds a Docker client pointed at a fake Docker HTTP server.-func newTestDockerClientInternal(t *testing.T, server *httptest.Server) *client.Client {+// newTestDockerClient builds a Docker client pointed at a fake Docker HTTP server.+func newTestDockerClient(t *testing.T, server *httptest.Server) *client.Client {t.Helper()httpClient := server.Client()@@ -402,8 +422,8 @@ func newTestDockerClientInternal(t *testing.T, server *httptest.Server) *client.return cli}-// setupProjectTestDBInternal builds an in-memory DB migrated for project-related tests.-func setupProjectTestDBInternal(t *testing.T) *database.DB {+// setupProjectTestDB builds an in-memory DB migrated for project-related tests.+func setupProjectTestDB(t *testing.T) *database.DB {t.Helper()db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})require.NoError(t, err)@@ -411,9 +431,9 @@ func setupProjectTestDBInternal(t *testing.T) *database.DB {return &database.DB{DB: db}}-var dockerAPIVersionPrefixInternal = regexp.MustCompile(`^/v\d+\.\d+`)+var dockerAPIVersionPrefix = regexp.MustCompile(`^/v\d+\.\d+`)-func TestApplyEditPreservesUnmanagedSettingsInternal(t *testing.T) {+func TestApplyEditPreservesUnmanagedSettings(t *testing.T) {cfg := container.Config{Image: "nginx:1.25",Env: []string{"OLD=1"},@@ -432,8 +452,9 @@ func TestApplyEditPreservesUnmanagedSettingsInternal(t *testing.T) {env := []string{"NEW=1"}req := containertypes.Edit{Environment: &env}- require.NoError(t, applyEditToContainerConfigInternal(&cfg, hc.PortBindings, req))- require.NoError(t, applyEditToHostConfigInternal(&hc, req))+ cfg, err := applyEditToContainerConfig(cfg, hc.PortBindings, req)+ require.NoError(t, err)+ require.NoError(t, applyEditToHostConfig(&hc, req))require.Equal(t, []string{"NEW=1"}, cfg.Env)require.Equal(t, "SIGQUIT", cfg.StopSignal)@@ -444,7 +465,7 @@ func TestApplyEditPreservesUnmanagedSettingsInternal(t *testing.T) {require.Contains(t, cfg.ExposedPorts, network.MustParsePort("80/tcp"))}-func TestMergeEditMountsPreservesMountOptionsInternal(t *testing.T) {+func TestMergeEditMountsPreservesMountOptions(t *testing.T) {existing := []mount.Mount{{Type: mount.TypeVolume,@@ -458,14 +479,14 @@ func TestMergeEditMountsPreservesMountOptionsInternal(t *testing.T) {// Round-trip the volume mount (toggling read-only) and drop the tmpfs one.requested := []containertypes.MountCreate{{Type: "volume", Source: "data", Target: "/data", ReadOnly: true}}- out := mergeEditMountsInternal(existing, requested)+ out := mergeEditMounts(existing, requested)require.Len(t, out, 1)require.True(t, out[0].ReadOnly)require.NotNil(t, out[0].VolumeOptions)require.Equal(t, map[string]string{"keep": "me"}, out[0].VolumeOptions.Labels)}-func TestMergeEditMountsAppliesSourceChangeInternal(t *testing.T) {+func TestMergeEditMountsAppliesSourceChange(t *testing.T) {existing := []mount.Mount{{Type: mount.TypeVolume,@@ -488,7 +509,7 @@ func TestMergeEditMountsAppliesSourceChangeInternal(t *testing.T) {{Type: "bind", Source: "/new/path", Target: "/config", ReadOnly: true},}- out := mergeEditMountsInternal(existing, requested)+ out := mergeEditMounts(existing, requested)require.Len(t, out, 2)require.Equal(t, "new-volume", out[0].Source)require.NotNil(t, out[0].VolumeOptions)@@ -499,7 +520,7 @@ func TestMergeEditMountsAppliesSourceChangeInternal(t *testing.T) {require.Equal(t, mount.PropagationRPrivate, out[1].BindOptions.Propagation)}-func TestApplyEditRecomputesExposedPortsInternal(t *testing.T) {+func TestApplyEditRecomputesExposedPorts(t *testing.T) {cfg := container.Config{ExposedPorts: network.PortSet{network.MustParsePort("80/tcp"): {},@@ -513,8 +534,9 @@ func TestApplyEditRecomputesExposedPortsInternal(t *testing.T) {newBindings := map[string][]containertypes.PortBindingCreate{"443/tcp": {{HostPort: "8443"}}}req := containertypes.Edit{HostConfig: &containertypes.HostConfigEdit{PortBindings: &newBindings}}- require.NoError(t, applyEditToContainerConfigInternal(&cfg, hc.PortBindings, req))- require.NoError(t, applyEditToHostConfigInternal(&hc, req))+ cfg, err := applyEditToContainerConfig(cfg, hc.PortBindings, req)+ require.NoError(t, err)+ require.NoError(t, applyEditToHostConfig(&hc, req))// new binding key + previously-exposed-but-unbound port survive; the old bound port is gonerequire.Contains(t, cfg.ExposedPorts, network.MustParsePort("443/tcp"))@@ -523,7 +545,7 @@ func TestApplyEditRecomputesExposedPortsInternal(t *testing.T) {require.Contains(t, hc.PortBindings, network.MustParsePort("443/tcp"))}-func TestBuildEditNetworkingConfigInternal(t *testing.T) {+func TestBuildEditNetworkingConfig(t *testing.T) {containerInspect := container.InspectResponse{HostConfig: &container.HostConfig{NetworkMode: "my-net"},NetworkSettings: &container.NetworkSettings{@@ -534,7 +556,7 @@ func TestBuildEditNetworkingConfigInternal(t *testing.T) {}// nil request preserves existing attachments- out, err := buildEditNetworkingConfigInternal(containerInspect, nil, "1.44")+ out, err := buildEditNetworkingConfig(containerInspect, nil, "1.44")require.NoError(t, err)require.NotNil(t, out)require.Contains(t, out.EndpointsConfig, "my-net")@@ -546,7 +568,7 @@ func TestBuildEditNetworkingConfigInternal(t *testing.T) {"other-net": {},},}- out, err = buildEditNetworkingConfigInternal(containerInspect, req, "1.44")+ out, err = buildEditNetworkingConfig(containerInspect, req, "1.44")require.NoError(t, err)require.Len(t, out.EndpointsConfig, 2)require.Equal(t, []string{"renamed"}, out.EndpointsConfig["my-net"].Aliases)@@ -555,17 +577,17 @@ func TestBuildEditNetworkingConfigInternal(t *testing.T) {// host network mode: endpoint edits are skipped entirelyhostModeInspect := container.InspectResponse{HostConfig: &container.HostConfig{NetworkMode: "host"}}- out, err = buildEditNetworkingConfigInternal(hostModeInspect, req, "1.44")+ out, err = buildEditNetworkingConfig(hostModeInspect, req, "1.44")require.NoError(t, err)require.Nil(t, out)}-func TestRecreateContainerDaemonCorrelationInternal(t *testing.T) {- db := setupProjectTestDBInternal(t)+func TestRecreateContainerDaemonCorrelation(t *testing.T) {+ db := setupProjectTestDB(t)events := event.NewEventService(db, nil, nil)observed := make(chan string, 8)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- path := dockerTestPathInternal(r.URL.Path)+ path := dockerTestPath(r.URL.Path)id, name := "old-id", "web"if path == "/containers/create" {id = ""@@ -591,7 +613,7 @@ func TestRecreateContainerDaemonCorrelationInternal(t *testing.T) {w.WriteHeader(http.StatusNoContent)}))t.Cleanup(server.Close)- dockerClient := newTestDockerClientInternal(t, server)+ dockerClient := newTestDockerClient(t, server)svc := NewContainerService(events, nil, nil, nil, nil)info := container.InspectResponse{ID: "old-id",@@ -600,7 +622,7 @@ func TestRecreateContainerDaemonCorrelationInternal(t *testing.T) {Config: &container.Config{Image: "app:latest"},HostConfig: &container.HostConfig{},}- id, err := svc.recreateContainerInternal(+ id, err := svc.recreateContainer(t.Context(),dockerClient,info,@@ -638,7 +660,9 @@ func TestRecreateContainerDaemonCorrelationInternal(t *testing.T) {}func TestBuildSummariesUsesContainerTagPolicyUpdates(t *testing.T) {- service := &ContainerService{}+ settingsService, err := settings.NewSettingsService(t.Context(), setupProjectTestDB(t))+ require.NoError(t, err)+ service := NewContainerService(nil, nil, nil, settingsService, nil)strategyLabel := "com.getarcaneapp.arcane.updater.strategy"containers := []container.Summary{{ID: "first", Image: "app:3.1.0", ImageID: "shared-image"},@@ -674,7 +698,7 @@ func TestBuildSummariesUsesContainerTagPolicyUpdates(t *testing.T) {require.Contains(t, string(encoded), `"autoUpdateEnabled":false`, "false status must stay serialized")}-func TestContainerServiceGetContainerProcessesInternal(t *testing.T) {+func TestContainerServiceGetContainerProcesses(t *testing.T) {tests := []struct {name stringstatus int@@ -732,7 +756,7 @@ func TestContainerServiceGetContainerProcessesInternal(t *testing.T) {t.Run(tt.name, func(t *testing.T) {var gotArgs []stringserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodGet || dockerTestPathInternal(r.URL.Path) != "/containers/container-1/top" {+ if r.Method != http.MethodGet || dockerTestPath(r.URL.Path) != "/containers/container-1/top" {http.NotFound(w, r)return}@@ -748,7 +772,9 @@ func TestContainerServiceGetContainerProcessesInternal(t *testing.T) {}}))t.Cleanup(server.Close)- svc := NewContainerService(nil, docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)), nil, nil, nil)+ settingsService, err := settings.NewSettingsService(t.Context(), setupProjectTestDB(t))+ require.NoError(t, err)+ svc := NewContainerService(nil, docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)), nil, settingsService, nil)processes, err := svc.GetContainerProcesses(t.Context(), "container-1")wantArgs := []string{strings.Join(containerProcessesPsArgs[0], " ")}@@ -767,18 +793,20 @@ func TestContainerServiceGetContainerProcessesInternal(t *testing.T) {}}-func TestContainerServiceGetContainerProcessesPropagatesContextInternal(t *testing.T) {+func TestContainerServiceGetContainerProcessesPropagatesContext(t *testing.T) {requestDone := make(chan struct{})server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {<-r.Context().Done()close(requestDone)}))t.Cleanup(server.Close)- svc := NewContainerService(nil, docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)), nil, nil, nil)+ settingsService, err := settings.NewSettingsService(t.Context(), setupProjectTestDB(t))+ require.NoError(t, err)+ svc := NewContainerService(nil, docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)), nil, settingsService, nil)ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)defer cancel()- _, err := svc.GetContainerProcesses(ctx, "container-1")+ _, err = svc.GetContainerProcesses(ctx, "container-1")require.ErrorIs(t, err, context.DeadlineExceeded)select {case <-requestDone:@@ -787,21 +815,8 @@ func TestContainerServiceGetContainerProcessesPropagatesContextInternal(t *testi}}-type resourceStatsServerInternal struct {- mu sync.Mutex- containers []container.Summary- stats map[string]container.StatsResponse- statsStatus map[string]int- statsCalls map[string]int- delay time.Duration- slowIDs map[string]bool-- current atomic.Int32- maxSeen atomic.Int32-}--func newResourceStatsServerInternal() *resourceStatsServerInternal {- return &resourceStatsServerInternal{+func newResourceStatsServer() *resourceStatsServer {+ return &resourceStatsServer{stats: map[string]container.StatsResponse{},statsStatus: map[string]int{},statsCalls: map[string]int{},@@ -809,7 +824,7 @@ func newResourceStatsServerInternal() *resourceStatsServerInternal {}}-func (f *resourceStatsServerInternal) addContainer(id, name, state string, labels ...map[string]string) {+func (f *resourceStatsServer) addContainer(id, name, state string, labels ...map[string]string) {f.mu.Lock()defer f.mu.Unlock()var l map[string]string@@ -819,7 +834,7 @@ func (f *resourceStatsServerInternal) addContainer(id, name, state string, labelf.containers = append(f.containers, container.Summary{ID: id, Names: []string{"/" + name}, State: container.ContainerState(state), Image: "img:latest", Labels: l})}-func (f *resourceStatsServerInternal) setStats(id string, cpuPercent float64, memoryUsage, memoryLimit uint64) {+func (f *resourceStatsServer) setStats(id string, cpuPercent float64, memoryUsage, memoryLimit uint64) {f.mu.Lock()defer f.mu.Unlock()// cpuPercent maps to cpuDelta/systemDelta*100 with a fixed systemDelta of 1000.@@ -840,15 +855,15 @@ func (f *resourceStatsServerInternal) setStats(id string, cpuPercent float64, me}}-func (f *resourceStatsServerInternal) callsFor(id string) int {+func (f *resourceStatsServer) callsFor(id string) int {f.mu.Lock()defer f.mu.Unlock()return f.statsCalls[id]}-func (f *resourceStatsServerInternal) handler() http.Handler {+func (f *resourceStatsServer) handler() http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- path := dockerTestPathInternal(r.URL.Path)+ path := dockerTestPath(r.URL.Path)switch {case path == "/containers/json":f.mu.Lock()@@ -920,29 +935,35 @@ func (f *resourceStatsServerInternal) handler() http.Handler {})}-func newResourceSortServiceInternal(t *testing.T, fixture *resourceStatsServerInternal) *ContainerService {+func newResourceSortService(t *testing.T, fixture *resourceStatsServer) *ContainerService {t.Helper()server := httptest.NewServer(fixture.handler())t.Cleanup(server.Close)+ db := setupProjectTestDB(t)+ settingsService, err := settings.NewSettingsService(t.Context(), db)+ require.NoError(t, err)+ dockerService := docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server))return NewContainerService(nil,- docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)),- nil,- nil,+ dockerService,+ imagedomain.NewImageService(db, dockerService, nil, nil, nil, nil, settingsService, nil),+ settingsService,nil,)}-func resourceSortParamsInternal(sort, order string, start, limit int) pagination.QueryParams {+func resourceSortParams(sort, order string, start, limit int) pagination.QueryParams {return pagination.QueryParams{- Sort: sort, Order: pagination.SortOrder(order),- Start: start, Limit: limit,+ Sort: sort,+ Order: pagination.SortOrder(order),+ Start: start,+ Limit: limit,Filters: map[string]string{},}}-func summaryIDsInternal(items []containertypes.Summary) []string {+func summaryIDs(items []containertypes.Summary) []string {ids := make([]string, 0, len(items))for _, item := range items {ids = append(ids, item.ID)@@ -950,7 +971,7 @@ func summaryIDsInternal(items []containertypes.Summary) []string {return ids}-func listResourceSortedInternal(t *testing.T, svc *ContainerService, params pagination.QueryParams) ContainerListResult {+func listResourceSorted(t *testing.T, svc *ContainerService, params pagination.QueryParams) ContainerListResult {t.Helper()result, err := svc.ListContainersPaginated(t.Context(), params, true, true, true, "")require.NoError(t, err)@@ -958,7 +979,7 @@ func listResourceSortedInternal(t *testing.T, svc *ContainerService, params pagi}func TestResourceSortRanksMemoryByBytesNotPercent(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()// high-percent: 800 of 1000 bytes (80%); high-bytes: 900 of 10000 (9%).fixture.addContainer("high-percent", "high-percent", "running")fixture.setStats("high-percent", 10, 800, 1000)@@ -966,19 +987,19 @@ func TestResourceSortRanksMemoryByBytesNotPercent(t *testing.T) {fixture.setStats("high-bytes", 10, 900, 10000)fixture.addContainer("opted-out", "opted-out", "running", map[string]string{"com.getarcaneapp.arcane.updater": "false"})fixture.setStats("opted-out", 10, 100, 10000)- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)- result := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))- require.Equal(t, []string{"high-bytes", "high-percent", "opted-out"}, summaryIDsInternal(result.Items))+ result := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))+ require.Equal(t, []string{"high-bytes", "high-percent", "opted-out"}, summaryIDs(result.Items))require.True(t, result.Items[0].AutoUpdateEnabled)require.False(t, result.Items[2].AutoUpdateEnabled)- result = listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "asc", 0, 20))- require.Equal(t, []string{"opted-out", "high-percent", "high-bytes"}, summaryIDsInternal(result.Items))+ result = listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "asc", 0, 20))+ require.Equal(t, []string{"opted-out", "high-percent", "high-bytes"}, summaryIDs(result.Items))- flat, err := svc.ListContainersPaginated(t.Context(), resourceSortParamsInternal("name", "asc", 0, 20), true, true, true, "")+ flat, err := svc.ListContainersPaginated(t.Context(), resourceSortParams("name", "asc", 0, 20), true, true, true, "")require.NoError(t, err)- require.Equal(t, []string{"high-bytes", "high-percent", "opted-out"}, summaryIDsInternal(flat.Items))+ require.Equal(t, []string{"high-bytes", "high-percent", "opted-out"}, summaryIDs(flat.Items))require.True(t, flat.Items[0].AutoUpdateEnabled)require.False(t, flat.Items[2].AutoUpdateEnabled)@@ -991,13 +1012,13 @@ func TestResourceSortRanksMemoryByBytesNotPercent(t *testing.T) {}func TestResourceSortOrdersAcrossPages(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()memoryByID := map[string]uint64{"c1": 100, "c2": 200, "c3": 300, "c4": 400, "c5": 500}for id, memory := range memoryByID {fixture.addContainer(id, id, "running")fixture.setStats(id, 0, memory, 10000)}- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)tests := []struct {name string@@ -1013,51 +1034,51 @@ func TestResourceSortOrdersAcrossPages(t *testing.T) {}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- result := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, tt.order, tt.start, 2))- require.Equal(t, tt.want, summaryIDsInternal(result.Items))+ result := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, tt.order, tt.start, 2))+ require.Equal(t, tt.want, summaryIDs(result.Items))require.Equal(t, int64(5), result.Pagination.TotalItems)})}}func TestResourceSortOrdersByCPUPercent(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("busy", "busy", "running")fixture.setStats("busy", 90, 100, 1000)fixture.addContainer("idle", "idle", "running")fixture.setStats("idle", 5, 100, 1000)- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)- result := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortCPUUsage, "desc", 0, 20))- require.Equal(t, []string{"busy", "idle"}, summaryIDsInternal(result.Items))+ result := listResourceSorted(t, svc, resourceSortParams(containertypes.SortCPUUsage, "desc", 0, 20))+ require.Equal(t, []string{"busy", "idle"}, summaryIDs(result.Items))require.InDelta(t, 90, result.Items[0].ResourceSample.CPUPercent, 0.2)require.InDelta(t, 5, result.Items[1].ResourceSample.CPUPercent, 0.2)}func TestResourceSortAppliesSearchAndFiltersBeforeCollectingStats(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("web", "web", "running", map[string]string{"team": "backend"})fixture.setStats("web", 10, 100, 1000)fixture.addContainer("db", "db", "running")fixture.setStats("db", 20, 200, 1000)- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)- params := resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20)+ params := resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20)params.Search = "web"- result := listResourceSortedInternal(t, svc, params)- require.Equal(t, []string{"web"}, summaryIDsInternal(result.Items))+ result := listResourceSorted(t, svc, params)+ require.Equal(t, []string{"web"}, summaryIDs(result.Items))require.Equal(t, 1, fixture.callsFor("web"))require.Equal(t, 0, fixture.callsFor("db"), "filtered-out containers must not trigger stats collection")- params = resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20)+ params = resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20)params.Filters["label"] = "team=backend"- result = listResourceSortedInternal(t, svc, params)- require.Equal(t, []string{"web"}, summaryIDsInternal(result.Items))+ result = listResourceSorted(t, svc, params)+ require.Equal(t, []string{"web"}, summaryIDs(result.Items))require.Equal(t, 0, fixture.callsFor("db"))}func TestResourceSortHandlesZeroUnavailableStoppedAndTies(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("zero", "zero", "running")fixture.setStats("zero", 0, 0, 1000)fixture.addContainer("tie-b", "alpha", "running")@@ -1067,53 +1088,53 @@ func TestResourceSortHandlesZeroUnavailableStoppedAndTies(t *testing.T) {fixture.addContainer("broken", "broken", "running")fixture.statsStatus["broken"] = http.StatusInternalServerErrorfixture.addContainer("stopped", "stopped", "exited")- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)- asc := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "asc", 0, 20))- require.Equal(t, []string{"zero", "tie-a", "tie-b", "broken", "stopped"}, summaryIDsInternal(asc.Items),+ asc := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "asc", 0, 20))+ require.Equal(t, []string{"zero", "tie-a", "tie-b", "broken", "stopped"}, summaryIDs(asc.Items),"zeros first, unavailable and stopped last, ties by name then ID")require.NotNil(t, asc.Items[0].ResourceSample, "zero usage is a valid sample, not unavailable")require.Nil(t, asc.Items[3].ResourceSample)require.Nil(t, asc.Items[4].ResourceSample)- desc := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))- require.Equal(t, []string{"tie-a", "tie-b", "zero", "broken", "stopped"}, summaryIDsInternal(desc.Items),+ desc := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))+ require.Equal(t, []string{"tie-a", "tie-b", "zero", "broken", "stopped"}, summaryIDs(desc.Items),"unavailable stays last in descending order too")require.Equal(t, 0, fixture.callsFor("stopped"), "stopped containers must not trigger stats collection")}func TestResourceSortCachesSamples(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("web", "web", "running")fixture.setStats("web", 10, 100, 1000)- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)for range 2 {- listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))+ listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))}require.Equal(t, 1, fixture.callsFor("web"), "second list within the TTL must reuse the cached sample")}func TestResourceSortCacheExpires(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("web", "web", "running")fixture.setStats("web", 10, 100, 1000)- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)svc.stats = stats.New(svc.dockerService.GetClient, svc.dockerService.DockerHost, 50*time.Millisecond, 0, 0)- listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))+ listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))time.Sleep(100 * time.Millisecond)- listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))+ listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))require.Equal(t, 2, fixture.callsFor("web"), "expired entries must be refetched")}func TestResourceSortCoalescesConcurrentFetches(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("web", "web", "running")fixture.setStats("web", 10, 100, 1000)fixture.delay = 100 * time.Millisecond- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)const callers = 8var wg sync.WaitGroup@@ -1132,54 +1153,54 @@ func TestResourceSortCoalescesConcurrentFetches(t *testing.T) {}func TestResourceSortBoundsCollectionConcurrency(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.delay = 50 * time.Millisecondfor i := range 32 {id := fmt.Sprintf("c%02d", i)fixture.addContainer(id, id, "running")fixture.setStats(id, 0, uint64(i), 10000)}- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)- result := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 40))+ result := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 40))require.Len(t, result.Items, 32)require.LessOrEqual(t, fixture.maxSeen.Load(), int32(stats.ContainerResourceCollectConcurrency))}func TestResourceSortBatchTimeoutFailsRequest(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("slow", "slow", "running")fixture.setStats("slow", 10, 100, 1000)fixture.delay = time.Second- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)svc.stats = stats.New(svc.dockerService.GetClient, svc.dockerService.DockerHost, stats.ContainerResourceSampleTTL, 0, 50*time.Millisecond)- _, err := svc.ListContainersPaginated(t.Context(), resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20), true, true, true, "")+ _, err := svc.ListContainersPaginated(t.Context(), resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20), true, true, true, "")require.Error(t, err, "batch timeout must fail the refresh instead of presenting a partial sort")}func TestResourceSortPerContainerTimeoutMarksUnavailable(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("fast", "fast", "running")fixture.setStats("fast", 10, 100, 1000)fixture.addContainer("slow", "slow", "running")fixture.setStats("slow", 10, 200, 1000)fixture.slowIDs["slow"] = true- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)svc.stats = stats.New(svc.dockerService.GetClient, svc.dockerService.DockerHost, stats.ContainerResourceSampleTTL, 50*time.Millisecond, 0)- result := listResourceSortedInternal(t, svc, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20))- require.Equal(t, []string{"fast", "slow"}, summaryIDsInternal(result.Items))+ result := listResourceSorted(t, svc, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20))+ require.Equal(t, []string{"fast", "slow"}, summaryIDs(result.Items))require.NotNil(t, result.Items[0].ResourceSample)require.Nil(t, result.Items[1].ResourceSample, "per-container timeout leaves the sample unavailable")}func TestResourceSortCancelledContextFailsRequest(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()fixture.addContainer("web", "web", "running")fixture.setStats("web", 10, 100, 1000)fixture.delay = time.Second- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)ctx, cancel := context.WithCancel(t.Context())go func() {@@ -1187,7 +1208,7 @@ func TestResourceSortCancelledContextFailsRequest(t *testing.T) {cancel()}()- _, err := svc.ListContainersPaginated(ctx, resourceSortParamsInternal(containertypes.SortMemoryUsage, "desc", 0, 20), true, true, true, "")+ _, err := svc.ListContainersPaginated(ctx, resourceSortParams(containertypes.SortMemoryUsage, "desc", 0, 20), true, true, true, "")require.Error(t, err)}@@ -1229,7 +1250,7 @@ func TestSampleResourcesFiltersRequestedIDs(t *testing.T) {}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- fixture := newResourceStatsServerInternal()+ fixture := newResourceStatsServer()for _, id := range tt.running {fixture.addContainer(id, id, "running")fixture.setStats(id, 10, 100, 1000)@@ -1238,7 +1259,7 @@ func TestSampleResourcesFiltersRequestedIDs(t *testing.T) {fixture.addContainer(id, id, "exited")fixture.setStats(id, 10, 100, 1000)}- svc := newResourceSortServiceInternal(t, fixture)+ svc := newResourceSortService(t, fixture)samples, err := svc.SampleResources(t.Context(), tt.request, stats.ContainerResourceSampleTTL)require.NoError(t, err)@@ -1250,10 +1271,10 @@ func TestSampleResourcesFiltersRequestedIDs(t *testing.T) {}}-func newLogsTestServiceInternal(t *testing.T, tty bool, logsBody []byte, gotQuery *url.Values) *ContainerService {+func newLogsTestService(t *testing.T, tty bool, logsBody []byte, gotQuery *url.Values) *ContainerService {t.Helper()server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- switch dockerTestPathInternal(r.URL.Path) {+ switch dockerTestPath(r.URL.Path) {case "/containers/container-1/json":ttyJSON := kit.Ternary(tty, "true", "false")_, _ = io.WriteString(w, `{"Id":"0123456789abcdef0123456789abcdef","Config":{"Tty":`+ttyJSON+`}}`)@@ -1267,15 +1288,15 @@ func newLogsTestServiceInternal(t *testing.T, tty bool, logsBody []byte, gotQuert.Cleanup(server.Close)return NewContainerService(- event.NewEventService(setupProjectTestDBInternal(t), nil, nil),- docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClientInternal(t, server)),+ event.NewEventService(setupProjectTestDB(t), nil, nil),+ docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)),nil,nil,nil,)}-func TestContainerServiceDownloadLogsDemultiplexesNonTTYInternal(t *testing.T) {+func TestContainerServiceDownloadLogsDemultiplexesNonTTY(t *testing.T) {var frames bytes.BufferlongLine := strings.Repeat("x", 70*1024) + "\n"for _, frame := range []struct {@@ -1296,7 +1317,7 @@ func TestContainerServiceDownloadLogsDemultiplexesNonTTYInternal(t *testing.T) {}var gotQuery url.Values- svc := newLogsTestServiceInternal(t, false, frames.Bytes(), &gotQuery)+ svc := newLogsTestService(t, false, frames.Bytes(), &gotQuery)reader, filename, err := svc.DownloadLogs(t.Context(), "container-1")require.NoError(t, err)@@ -1316,9 +1337,9 @@ func TestContainerServiceDownloadLogsDemultiplexesNonTTYInternal(t *testing.T) {require.Empty(t, gotQuery.Get("since"))}-func TestContainerServiceDownloadLogsPassesTTYOutputThroughInternal(t *testing.T) {+func TestContainerServiceDownloadLogsPassesTTYOutputThrough(t *testing.T) {var gotQuery url.Values- svc := newLogsTestServiceInternal(t, true, []byte("raw tty line\r\nno trailing newline"), &gotQuery)+ svc := newLogsTestService(t, true, []byte("raw tty line\r\nno trailing newline"), &gotQuery)reader, filename, err := svc.DownloadLogs(t.Context(), "container-1")require.NoError(t, err)diff --git a/backend/internal/environment/children/health/service.go b/backend/internal/environment/children/health/service.goindex 6a4080288..59e4914b8 100644--- a/backend/internal/environment/children/health/service.go+++ b/backend/internal/environment/children/health/service.go@@ -31,20 +31,49 @@ func New(httpClient *http.Client, dockerService *docker.DockerClientService) *Se// API URL always probes directly over HTTP.func (s *Service) Probe(ctx context.Context, id, apiURL string, isEdge, customURL bool) (string, error) {if id == "0" && !customURL {- return s.probeLocalDockerInternal(ctx)+ reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second)+ defer cancel()++ dockerClient, err := s.dockerService.GetClient(ctx)+ if err != nil {+ return "offline", fmt.Errorf("failed to connect to Docker: %w", err)+ }+ if _, pingErr := dockerClient.Ping(reqCtx, client.PingOptions{}); pingErr != nil {+ return "offline", fmt.Errorf("docker ping failed: %w", pingErr)+ }+ return "online", nil}if isEdge && !customURL {- return probeEdgeInternal(ctx, id)+ // The probe is standing tunnel demand, keeping idle poll-mode tunnels+ // open across check cycles instead of letting them flap.+ edge.TouchTunnelDemand(id, edge.DefaultTunnelDemandTTL)+ if !edge.HasActiveTunnel(id) {+ if _, ok := edge.RequestTunnelAndWait(ctx, id, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()).Get(); !ok {+ return "offline", errors.New("edge agent is not connected")+ }+ }++ reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)+ defer cancel()++ statusCode, _, err := edge.DoRequest(reqCtx, id, http.MethodGet, "/api/health", nil)+ if err != nil {+ return "offline", fmt.Errorf("health check via tunnel failed: %w", err)+ }+ if statusCode == http.StatusOK {+ return "online", nil+ }+ return "error", fmt.Errorf("unexpected status code: %d", statusCode)}- healthURL, err := buildEndpointURLInternal(apiURL, "/api/health")+ baseURL, err := httpx.NormalizeBaseURL(apiURL)if err != nil {return "offline", fmt.Errorf("invalid environment API URL: %w", err)}reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)defer cancel()- req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, healthURL, http.NoBody)+ req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, strings.TrimRight(baseURL, "/")+"/api/health", http.NoBody)if err != nil {return "offline", fmt.Errorf("failed to create request: %w", err)}@@ -59,50 +88,3 @@ func (s *Service) Probe(ctx context.Context, id, apiURL string, isEdge, customUR}return "error", fmt.Errorf("unexpected status code: %d", resp.StatusCode)}--// probeEdgeInternal checks an edge agent through its tunnel. The probe itself-// is standing demand for the tunnel, which keeps idle poll-mode tunnels open-// across check cycles instead of letting them flap between checks.-func probeEdgeInternal(ctx context.Context, id string) (string, error) {- edge.TouchTunnelDemand(id, edge.DefaultTunnelDemandTTL)- if !edge.HasActiveTunnel(id) {- if _, ok := edge.RequestTunnelAndWait(ctx, id, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()).Get(); !ok {- return "offline", errors.New("edge agent is not connected")- }- }-- reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)- defer cancel()-- statusCode, _, err := edge.DoRequest(reqCtx, id, http.MethodGet, "/api/health", nil)- if err != nil {- return "offline", fmt.Errorf("health check via tunnel failed: %w", err)- }- if statusCode == http.StatusOK {- return "online", nil- }- return "error", fmt.Errorf("unexpected status code: %d", statusCode)-}--func (s *Service) probeLocalDockerInternal(ctx context.Context) (string, error) {- reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second)- defer cancel()-- dockerClient, err := s.dockerService.GetClient(ctx)- if err != nil {- return "offline", fmt.Errorf("failed to connect to Docker: %w", err)- }- if _, pingErr := dockerClient.Ping(reqCtx, client.PingOptions{}); pingErr != nil {- return "offline", fmt.Errorf("docker ping failed: %w", pingErr)- }- return "online", nil-}--func buildEndpointURLInternal(apiURL, endpointPath string) (string, error) {- baseURL, err := httpx.NormalizeBaseURL(apiURL)- if err != nil {- return "", err- }-- return strings.TrimRight(baseURL, "/") + endpointPath, nil-}diff --git a/backend/internal/environment/children/proxy/service.go b/backend/internal/environment/children/proxy/service.goindex 6c39360c9..90a1e114b 100644--- a/backend/internal/environment/children/proxy/service.go+++ b/backend/internal/environment/children/proxy/service.go@@ -8,7 +8,6 @@ import ("errors""fmt""log/slog"- "maps""net/http""strings""time"@@ -16,6 +15,7 @@ import ("github.com/getarcaneapp/arcane/types/v2/backup""github.com/getarcaneapp/arcane/types/v2/containerregistry""github.com/getarcaneapp/arcane/types/v2/gitops"+ kit "go.getarcane.app/kit/pkg""go.getarcane.app/sys/crypto""github.com/getarcaneapp/arcane/backend/v2/internal/database"@@ -40,29 +40,59 @@ type Service struct {syncGate *utils.SyncGate}+// Target is a validated remote environment endpoint; build it with NewTarget.+type Target struct {+ id string+ name string+ isEdge bool+ accessToken *string+ targetURL string+}+// New builds the remote transport. syncGate is owned by the parent so it can// forget delivery state when an environment reconnects.func New(db *database.DB, httpClient *http.Client, settingsService *settings.SettingsService, syncGate *utils.SyncGate) *Service {return &Service{db: db,client: remenv.NewClient(httpClient, remenv.TunnelTransportFuncs{- EnsureAvailableFunc: ensureTunnelAvailableInternal,- DoFunc: doTunnelRequestInternal,+ EnsureAvailableFunc: func(ctx context.Context, envID string) error {+ if edge.HasActiveTunnel(envID) {+ return nil+ }+ if _, ok := edge.RequestTunnelAndWait(ctx, envID, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()).Get(); ok {+ return nil+ }+ return remenv.ErrEnvironmentUnavailable+ },+ DoFunc: func(ctx context.Context, envID, method, path string, headers map[string]string, body []byte) (*remenv.Response, error) {+ tunnel, ok := edge.GetRegistry().Get(envID).Get()+ if !ok {+ return nil, fmt.Errorf("no active tunnel for environment %s: %w", envID, remenv.ErrEnvironmentUnavailable)+ }+ if tunnel.Conn.IsClosed() {+ return nil, fmt.Errorf("tunnel for environment %s is closed: %w", envID, remenv.ErrEnvironmentUnavailable)+ }++ statusCode, respHeaders, respBody, err := edge.ProxyRequest(ctx, tunnel, method, path, "", headers, body)+ if err != nil {+ if errors.Is(err, edge.ErrTunnelConnectionClosed) {+ err = errors.Join(remenv.ErrEnvironmentUnavailable, err)+ }+ return nil, fmt.Errorf("tunnel request failed: %w", err)+ }++ return &remenv.Response{+ StatusCode: statusCode,+ Body: respBody,+ Headers: respHeaders,+ }, nil+ },}),settingsService: settingsService,syncGate: syncGate,}}-// Target is a validated remote environment endpoint; build it with NewTarget.-type Target struct {- id string- name string- isEdge bool- accessToken *string- targetURL string-}-// NewTarget validates a remote environment's endpoint. The local environment// is never a proxy target.func NewTarget(id, name, apiURL string, isEdge bool, accessToken *string) (*Target, error) {@@ -90,33 +120,29 @@ func NewTarget(id, name, apiURL string, isEdge bool, accessToken *string) (*Targ// Context bounds a proxied request by the configured proxy timeout.func (s *Service) Context(ctx context.Context) (context.Context, context.CancelFunc) {- if s != nil && s.settingsService != nil {- cfg := s.settingsService.GetSettingsConfig()- return context.WithTimeout(ctx, timeouts.GetDuration(cfg.ProxyRequestTimeout.AsInt(), timeouts.DefaultProxyRequest))- }-- return context.WithTimeout(ctx, timeouts.DefaultProxyRequest)+ return context.WithTimeout(ctx, timeouts.GetDuration(s.settingsService.GetSettingsConfig().ProxyRequestTimeout.AsInt(), timeouts.DefaultProxyRequest))}-func buildRemoteRequestInternal(+// Execute sends one request to the target agent.+func (s *Service) Execute(+ ctx context.Context,target *Target,method string,path string,body []byte,- headers map[string]string,-) (remenv.Request, error) {- if target == nil {- return remenv.Request{}, errors.New("remote environment target is required")+) (*remenv.Response, error) {+ // Forward the activity batch ID so bulk actions proxied to a remote+ // environment group the same way they do locally.+ requestHeaders := make(map[string]string, 3)+ if batchID := utils.ActivityBatchIDFromContext(ctx); batchID != "" {+ requestHeaders[middleware.HeaderActivityBatchID] = batchID}-- requestHeaders := make(map[string]string, len(headers)+2)- maps.Copy(requestHeaders, headers)- if len(body) > 0 && method != http.MethodGet && requestHeaders["Content-Type"] == "" {+ if len(body) > 0 && method != http.MethodGet {requestHeaders["Content-Type"] = "application/json"}remenv.ApplyAgentTokenHeaderMap(requestHeaders, target.accessToken)- return remenv.Request{+ resp, err := s.client.Do(ctx, remenv.Request{EnvironmentID: target.id,IsEdge: target.isEdge,Method: method,@@ -124,29 +150,7 @@ func buildRemoteRequestInternal(Path: path,Headers: requestHeaders,Body: body,- }, nil-}--// Execute sends one request to the target agent.-func (s *Service) Execute(- ctx context.Context,- target *Target,- method string,- path string,- body []byte,-) (*remenv.Response, error) {- // Forward the activity batch ID so bulk actions proxied to a remote- // environment group the same way they do locally.- var headers map[string]string- if batchID := utils.ActivityBatchIDFromContext(ctx); batchID != "" {- headers = map[string]string{middleware.HeaderActivityBatchID: batchID}- }- request, err := buildRemoteRequestInternal(target, method, path, body, headers)- if err != nil {- return nil, err- }-- resp, err := s.client.Do(ctx, request)+ })if err != nil {return nil, fmt.Errorf("failed to send request to environment %s: %w", target.name, err)}@@ -191,38 +195,28 @@ func (s *Service) SyncRegistries(ctx context.Context, target *Target) error {return containerregistry.Sync{}, false, fmt.Errorf("normalize registry type for sync %s: %w", reg.ID, typeErr)}- syncItem := containerregistry.Sync{- ID: reg.ID,- URL: reg.URL,- Description: reg.Description,- Insecure: reg.Insecure,- Enabled: reg.Enabled,- RegistryType: registryType,- RepositoryNames: reg.RepositoryNames,- CreatedAt: reg.CreatedAt,- UpdatedAt: reg.UpdatedAt,- }-- if registryType == registry.RegistryTypeECR {- decryptedSecret, err := crypto.Decrypt(reg.AWSSecretAccessKey)- if err != nil {- return containerregistry.Sync{}, false, fmt.Errorf("failed to decrypt ECR secret for registry %s for sync: %w", reg.ID, err)- }-- syncItem.AWSAccessKeyID = reg.AWSAccessKeyID- syncItem.AWSSecretAccessKey = decryptedSecret- syncItem.AWSRegion = reg.AWSRegion- } else {- decryptedToken, err := crypto.Decrypt(reg.Token)- if err != nil {- return containerregistry.Sync{}, false, fmt.Errorf("failed to decrypt token for registry %s for sync: %w", reg.ID, err)- }-- syncItem.Username = reg.Username- syncItem.Token = decryptedToken+ decrypted, err := crypto.Decrypt(kit.Ternary(registryType == registry.RegistryTypeECR, reg.AWSSecretAccessKey, reg.Token))+ if err != nil {+ return containerregistry.Sync{}, false, fmt.Errorf("failed to decrypt %s for registry %s for sync: %w",+ kit.Ternary(registryType == registry.RegistryTypeECR, "ECR secret", "token"), reg.ID, err)}- return syncItem, true, nil+ return containerregistry.Sync{+ ID: reg.ID,+ URL: reg.URL,+ Username: kit.Ternary(registryType == registry.RegistryTypeECR, "", reg.Username),+ Token: kit.Ternary(registryType == registry.RegistryTypeECR, "", decrypted),+ Description: reg.Description,+ Insecure: reg.Insecure,+ Enabled: reg.Enabled,+ RegistryType: registryType,+ RepositoryNames: reg.RepositoryNames,+ AWSAccessKeyID: kit.Ternary(registryType == registry.RegistryTypeECR, reg.AWSAccessKeyID, ""),+ AWSSecretAccessKey: kit.Ternary(registryType == registry.RegistryTypeECR, decrypted, ""),+ AWSRegion: kit.Ternary(registryType == registry.RegistryTypeECR, reg.AWSRegion, ""),+ CreatedAt: reg.CreatedAt,+ UpdatedAt: reg.UpdatedAt,+ }, true, nil},func(items []containerregistry.Sync) containerregistry.SyncRequest {return containerregistry.SyncRequest{Registries: items}@@ -250,56 +244,51 @@ func (s *Service) SyncS3Destinations(ctx context.Context, target *Target) errorfunc (s *Service) SyncRepositories(ctx context.Context, target *Target) error {return s.fanOutSyncToEnvironment(ctx, target, "git repositories", "/api/git-repositories/sync",func(repo gitrepo.GitRepository) (gitops.RepositorySync, bool, error) {- item := gitops.RepositorySync{+ decrypt := func(value, what string) (string, error) {+ if value == "" {+ return "", nil+ }+ plain, err := crypto.Decrypt(value)+ if err != nil {+ return "", fmt.Errorf("failed to decrypt %s for repository %s for sync: %w", what, repo.ID, err)+ }+ return plain, nil+ }+ token, err := decrypt(repo.Token, "token")+ if err != nil {+ return gitops.RepositorySync{}, false, err+ }+ sshKey, err := decrypt(repo.SSHKey, "SSH key")+ if err != nil {+ return gitops.RepositorySync{}, false, err+ }+ signingKey, err := decrypt(repo.SigningKey, "signing key")+ if err != nil {+ return gitops.RepositorySync{}, false, err+ }+ passphrase, err := decrypt(repo.SigningKeyPassphrase, "signing key passphrase")+ if err != nil {+ return gitops.RepositorySync{}, false, err+ }++ return gitops.RepositorySync{ID: repo.ID,Name: repo.Name,URL: repo.URL,AuthType: repo.AuthType,Username: repo.Username,+ Token: token,+ SSHKey: sshKey,SSHHostKeyVerification: repo.SSHHostKeyVerification,CommitAuthorName: repo.CommitAuthorName,CommitAuthorEmail: repo.CommitAuthorEmail,+ SigningKey: signingKey,+ SigningKeyPassphrase: passphrase,Description: repo.Description,Enabled: repo.Enabled,CreatedAt: repo.CreatedAt,- }- if repo.UpdatedAt != nil {- item.UpdatedAt = *repo.UpdatedAt- }-- if repo.Token != "" {- decryptedToken, err := crypto.Decrypt(repo.Token)- if err != nil {- return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt token for repository %s for sync: %w", repo.ID, err)- }- item.Token = decryptedToken- }-- if repo.SSHKey != "" {- decryptedSSHKey, err := crypto.Decrypt(repo.SSHKey)- if err != nil {- return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt SSH key for repository %s for sync: %w", repo.ID, err)- }- item.SSHKey = decryptedSSHKey- }-- if repo.SigningKey != "" {- decryptedSigningKey, err := crypto.Decrypt(repo.SigningKey)- if err != nil {- return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt signing key for repository %s for sync: %w", repo.ID, err)- }- item.SigningKey = decryptedSigningKey- }-- if repo.SigningKeyPassphrase != "" {- decryptedPassphrase, err := crypto.Decrypt(repo.SigningKeyPassphrase)- if err != nil {- return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt signing key passphrase for repository %s for sync: %w", repo.ID, err)- }- item.SigningKeyPassphrase = decryptedPassphrase- }-- return item, true, nil+ UpdatedAt: kit.FromPtr(repo.UpdatedAt),+ }, true, nil},func(items []gitops.RepositorySync) gitops.RepositorySyncRequest {return gitops.RepositorySyncRequest{Repositories: items}@@ -319,7 +308,6 @@ func (s *Service) fanOutSyncToEnvironment[Model, Item, Request any](toSyncItem func(Model) (Item, bool, error),wrap func([]Item) Request,) error {- environmentID := target.idvar records []Modelif loadRecordsErr := s.db.WithContext(ctx).Find(&records).Error; loadRecordsErr != nil {return fmt.Errorf("failed to get %s: %w", kind, loadRecordsErr)@@ -340,18 +328,18 @@ func (s *Service) fanOutSyncToEnvironment[Model, Item, Request any](if err != nil {return fmt.Errorf("failed to marshal sync request: %w", err)}- unchanged, finishDelivery, err := s.syncGate.Begin(ctx, environmentID, path, reqBody)+ unchanged, finishDelivery, err := s.syncGate.Begin(ctx, target.id, path, reqBody)if err != nil {return fmt.Errorf("sync cancelled while waiting for an in-flight delivery: %w", err)}if unchanged {- slog.DebugContext(ctx, "Skipping sync; payload unchanged since last delivery", "kind", kind, "environmentId", environmentID, "environmentName", target.name)+ slog.DebugContext(ctx, "Skipping sync; payload unchanged since last delivery", "kind", kind, "environmentId", target.id, "environmentName", target.name)return nil}delivered := falsedefer func() { finishDelivery(delivered) }()- slog.InfoContext(ctx, "Starting sync to environment", "kind", kind, "environmentId", environmentID, "environmentName", target.name, "apiUrl", target.targetURL)+ slog.InfoContext(ctx, "Starting sync to environment", "kind", kind, "environmentId", target.id, "environmentName", target.name, "apiUrl", target.targetURL)reqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)defer cancel()@@ -373,50 +361,7 @@ func (s *Service) fanOutSyncToEnvironment[Model, Item, Request any](}delivered = true- slog.InfoContext(ctx, "Successfully synced to environment", "kind", kind, "environmentId", environmentID, "environmentName", target.name)+ slog.InfoContext(ctx, "Successfully synced to environment", "kind", kind, "environmentId", target.id, "environmentName", target.name)return nil}--func doTunnelRequestInternal(- ctx context.Context,- envID string,- method string,- path string,- headers map[string]string,- body []byte,-) (*remenv.Response, error) {- tunnel, ok := edge.GetRegistry().Get(envID).Get()- if !ok {- return nil, fmt.Errorf("no active tunnel for environment %s: %w", envID, remenv.ErrEnvironmentUnavailable)- }- if tunnel.Conn.IsClosed() {- return nil, fmt.Errorf("tunnel for environment %s is closed: %w", envID, remenv.ErrEnvironmentUnavailable)- }-- statusCode, respHeaders, respBody, err := edge.ProxyRequest(ctx, tunnel, method, path, "", headers, body)- if err != nil {- if errors.Is(err, edge.ErrTunnelConnectionClosed) {- err = errors.Join(remenv.ErrEnvironmentUnavailable, err)- }- return nil, fmt.Errorf("tunnel request failed: %w", err)- }-- return &remenv.Response{- StatusCode: statusCode,- Body: respBody,- Headers: respHeaders,- }, nil-}--func ensureTunnelAvailableInternal(ctx context.Context, envID string) error {- if edge.HasActiveTunnel(envID) {- return nil- }-- if _, ok := edge.RequestTunnelAndWait(ctx, envID, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()).Get(); ok {- return nil- }-- return remenv.ErrEnvironmentUnavailable-}diff --git a/backend/internal/environment/children/snippets/service.go b/backend/internal/environment/children/snippets/service.goindex 3619847c9..0766ede94 100644--- a/backend/internal/environment/children/snippets/service.go+++ b/backend/internal/environment/children/snippets/service.go@@ -119,7 +119,32 @@ func (s *Service) GenerateMTLS(ctx context.Context, edgeCfg *edge.Config, envID,if generatedAssets == nil {return nil, "", ""}- s.logGeneratedMTLSEvents(ctx, envID, envName, generatedAssets)+ if generatedAssets.CAGenerated {+ if _, caEventErr := s.eventService.CreateEvent(ctx, event.CreateEventRequest{+ Type: event.EventTypeEnvironmentMTLSCAGenerated,+ Severity: event.EventSeverityInfo,+ Title: "Edge mTLS CA generated",+ Description: "Arcane generated a new edge mTLS certificate authority",+ Metadata: database.JSON{"kind": "ca"},+ }); caEventErr != nil {+ slog.WarnContext(ctx, "Failed to create edge mTLS CA generation event", "error", caEventErr)+ }+ }+ if generatedAssets.CertIssued {+ if _, certEventErr := s.eventService.CreateEvent(ctx, event.CreateEventRequest{+ Type: event.EventTypeEnvironmentMTLSCertIssued,+ Severity: event.EventSeverityInfo,+ Title: "Edge mTLS certificate issued",+ Description: fmt.Sprintf("Arcane issued an edge mTLS client certificate for environment '%s'", envName),+ ResourceType: new("environment"),+ ResourceID: new(envID),+ ResourceName: new(envName),+ EnvironmentID: new(envID),+ Metadata: database.JSON{"kind": "client"},+ }); certEventErr != nil {+ slog.WarnContext(ctx, "Failed to create edge mTLS certificate issuance event", "environmentId", envID, "error", certEventErr)+ }+ }mtlsDockerRun := strings.Join([]string{"docker run -d \\",@@ -161,39 +186,6 @@ func (s *Service) GenerateMTLS(ctx context.Context, edgeCfg *edge.Config, envID,return generatedAssets, mtlsDockerRun, mtlsDockerCompose}-func (s *Service) logGeneratedMTLSEvents(ctx context.Context, envID, envName string, assets *edge.GeneratedMTLSAssets) {- if s == nil || s.eventService == nil || assets == nil {- return- }- if assets.CAGenerated {- if _, err := s.eventService.CreateEvent(ctx, event.CreateEventRequest{- Type: event.EventTypeEnvironmentMTLSCAGenerated,- Severity: event.EventSeverityInfo,- Title: "Edge mTLS CA generated",- Description: "Arcane generated a new edge mTLS certificate authority",- Metadata: database.JSON{"kind": "ca"},- }); err != nil {- slog.WarnContext(ctx, "Failed to create edge mTLS CA generation event", "error", err)- }- }- if assets.CertIssued {- envIDCopy := envID- if _, err := s.eventService.CreateEvent(ctx, event.CreateEventRequest{- Type: event.EventTypeEnvironmentMTLSCertIssued,- Severity: event.EventSeverityInfo,- Title: "Edge mTLS certificate issued",- Description: fmt.Sprintf("Arcane issued an edge mTLS client certificate for environment '%s'", envName),- ResourceType: new("environment"),- ResourceID: &envIDCopy,- ResourceName: new(envName),- EnvironmentID: &envIDCopy,- Metadata: database.JSON{"kind": "client"},- }); err != nil {- slog.WarnContext(ctx, "Failed to create edge mTLS certificate issuance event", "environmentId", envID, "error", err)- }- }-}-func AgentHostPort(agentURL string) string {parsed, err := url.Parse(strings.TrimSpace(agentURL))if err != nil || parsed.Host == "" {diff --git a/backend/internal/environment/children/swarm/helpers.go b/backend/internal/environment/children/swarm/helpers.godeleted file mode 100644index cdb9ca6f5..000000000--- a/backend/internal/environment/children/swarm/helpers.go+++ /dev/null@@ -1,24 +0,0 @@-package swarm--import (- "strings"-)--func buildAgentURLInternal(nodeID string) string {- shortNodeID := nodeID- if len(shortNodeID) > 12 {- shortNodeID = shortNodeID[:12]- }- return "edge://swarm-node-" + shortNodeID-}--func buildAgentNameInternal(hostname, nodeID string) string {- trimmedHostname := strings.TrimSpace(hostname)- if trimmedHostname != "" {- return trimmedHostname- }- if len(nodeID) > 12 {- nodeID = nodeID[:12]- }- return "Swarm Node " + nodeID-}diff --git a/backend/internal/environment/children/swarm/service.go b/backend/internal/environment/children/swarm/service.goindex 91ce11d01..85c73b3c1 100644--- a/backend/internal/environment/children/swarm/service.go+++ b/backend/internal/environment/children/swarm/service.go@@ -3,6 +3,7 @@package swarmimport (+ "cmp""context""errors""fmt"@@ -10,6 +11,8 @@ import ("strings""github.com/getarcaneapp/arcane/types/v2/environment"+ "github.com/samber/mo"+ kit "go.getarcane.app/kit/pkg""github.com/getarcaneapp/arcane/backend/v2/internal/apikey")@@ -31,7 +34,7 @@ func (s *Service) AgentIdentity(parentEnvironmentID, nodeID, hostname string) (sif strings.TrimSpace(nodeID) == "" {return "", "", errors.New("swarm node ID is required")}- return buildAgentNameInternal(hostname, nodeID), buildAgentURLInternal(nodeID), nil+ return cmp.Or(strings.TrimSpace(hostname), "Swarm Node "+nodeID[:min(len(nodeID), 12)]), "edge://swarm-node-" + nodeID[:min(len(nodeID), 12)], nil}// AuthorizeBinding rejects bindings that would silently move an environment or@@ -50,9 +53,8 @@ func (s *Service) AuthorizeBinding(return errors.New("disabled environments cannot be attached to swarm nodes")}- boundElsewhere := candidate.ParentEnvironmentID != nil && candidate.SwarmNodeID != nil &&- (strings.TrimSpace(*candidate.ParentEnvironmentID) != parentEnvironmentID || strings.TrimSpace(*candidate.SwarmNodeID) != nodeID)- if boundElsewhere && !rebind {+ if candidate.ParentEnvironmentID != nil && candidate.SwarmNodeID != nil && !rebind &&+ (strings.TrimSpace(*candidate.ParentEnvironmentID) != parentEnvironmentID || strings.TrimSpace(*candidate.SwarmNodeID) != nodeID) {return errors.New("environment is already bound to another swarm node; explicit rebinding is required")}@@ -75,14 +77,10 @@ func (s *Service) EnsureApiKey(rotate bool,link func(ctx context.Context, apiKeyID, apiKey string) error,) (string, error) {- if !rotate && accessToken != nil && strings.TrimSpace(*accessToken) != "" {+ if !rotate && strings.TrimSpace(kit.FromPtr(accessToken)) != "" {return strings.TrimSpace(*accessToken), nil}- if s.apiKeys == nil {- return "", errors.New("api key service not configured")- }-apiKeyDto, err := s.apiKeys.CreateEnvironmentApiKey(ctx, environmentID)if err != nil {return "", fmt.Errorf("failed to create environment API key: %w", err)@@ -102,7 +100,7 @@ func (s *Service) EnsureApiKey(// rejected. Failure is non-fatal for the rotation itself, but the old key// remains a valid credential until deleted, so log it as an error; the key// stays visible and deletable on the API Keys page.- if previousApiKeyID != nil && *previousApiKeyID != apiKeyDto.ID {+ if mo.PointerToOption(previousApiKeyID).OrElse(apiKeyDto.ID) != apiKeyDto.ID {if deleteApiKeyErr := s.apiKeys.DeleteApiKey(ctx, *previousApiKeyID); deleteApiKeyErr != nil && !errors.Is(deleteApiKeyErr, apikey.ErrApiKeyNotFound) {slog.ErrorContext(ctx, "Failed to delete previous environment API key; the old key remains valid until deleted manually", "environmentId", environmentID, "error", deleteApiKeyErr.Error())}diff --git a/backend/internal/environment/handler.go b/backend/internal/environment/handler.goindex 4a174aedd..bc204fbf2 100644--- a/backend/internal/environment/handler.go+++ b/backend/internal/environment/handler.go@@ -24,6 +24,7 @@ import ("github.com/getarcaneapp/arcane/types/v2/base""github.com/getarcaneapp/arcane/types/v2/environment""github.com/getarcaneapp/arcane/types/v2/version"+ "github.com/samber/mo"kit "go.getarcane.app/kit/pkg""go.getarcane.app/kit/pkg/mapping""go.getarcane.app/streams/agg"@@ -181,14 +182,15 @@ func (h *EnvironmentHandler) ListEnvironments(ctx context.Context, input *ListEnif err != nil {return nil, huma.Error500InternalServerError("Failed to fetch environments")}- for i := range envs {- ApplyEnvironmentRuntimeState(&envs[i])+ rows := make([]environment.Environment, 0, len(envs))+ for _, env := range envs {+ rows = append(rows, ApplyEnvironmentRuntimeState(env))}return &handlerutil.Page[environment.Environment]{Body: base.Paginated[environment.Environment]{Success: true,- Data: envs,+ Data: rows,Pagination: handlerutil.PaginationResponse(paginationResp),},}, nil@@ -216,7 +218,60 @@ func (h *EnvironmentHandler) RunStreamProducer(ctx context.Context, ps *authz.Pevar lastSentAt time.TimeaccessibleEnvIDs := accessibleEnvironmentIDs(ps)- h.streamHub.Subscribe(ctx, environmentStreamHubKey, h.runEnvironmentStreamLister, func(all []environment.Environment) bool {+ h.streamHub.Subscribe(ctx, environmentStreamHubKey, func(ctx context.Context, publish func([]environment.Environment)) {+ changes, unsubscribe := h.environmentService.SubscribeRuntimeChanges()+ defer unsubscribe()++ var rows []environment.Environment+ loaded := false+ publishRuntime := func() {+ if !loaded {+ return+ }+ envs := make([]environment.Environment, 0, len(rows))+ for _, row := range rows {+ envs = append(envs, ApplyEnvironmentRuntimeState(row))+ }+ publish(envs)+ }+ reload := func() {+ envs, err := h.environmentService.ListVisibleEnvironments(ctx)+ if err != nil {+ // 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+ }+ rows = envs+ loaded = true+ publishRuntime()+ }++ reload()++ runtimeTicker := time.NewTicker(environmentStreamRuntimeInterval)+ defer runtimeTicker.Stop()+ reloadTicker := time.NewTicker(environmentStreamReloadInterval)+ defer reloadTicker.Stop()++ for {+ select {+ case <-ctx.Done():+ return+ case <-changes:+ reload()+ case <-reloadTicker.C:+ reload()+ case <-runtimeTicker.C:+ if !loaded {+ reload()+ continue+ }+ publishRuntime()+ }+ }+ }, func(all []environment.Environment) bool {// all is shared across subscribers, so filter a copy.envs := allif accessibleEnvIDs != nil {@@ -239,61 +294,6 @@ func (h *EnvironmentHandler) RunStreamProducer(ctx context.Context, ps *authz.Pe})}-func (h *EnvironmentHandler) runEnvironmentStreamLister(ctx context.Context, publish func([]environment.Environment)) {- changes, unsubscribe := h.environmentService.SubscribeRuntimeChanges()- defer unsubscribe()-- 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 wake-up retries.- if ctx.Err() == nil {- slog.WarnContext(ctx, "environment stream failed to list environments", "error", err)- }- return- }- rows = envs- loaded = true- publishRuntime()- }-- reload()-- runtimeTicker := time.NewTicker(environmentStreamRuntimeInterval)- defer runtimeTicker.Stop()- reloadTicker := time.NewTicker(environmentStreamReloadInterval)- defer reloadTicker.Stop()-- for {- select {- case <-ctx.Done():- return- case <-changes:- reload()- case <-reloadTicker.C:- reload()- case <-runtimeTicker.C:- if !loaded {- reload()- continue- }- publishRuntime()- }- }-}-// CreateEnvironment creates a new environment.func (h *EnvironmentHandler) CreateEnvironment(ctx context.Context, input *CreateEnvironmentInput) (*handlerutil.Out[EnvironmentWithApiKey], error) {user, err := handlerutil.RequireUser(ctx)@@ -301,45 +301,37 @@ func (h *EnvironmentHandler) CreateEnvironment(ctx context.Context, input *Creatreturn nil, err}- env := &Environment{- ApiUrl: input.Body.ApiUrl,- Enabled: true,- }- if input.Body.Name != nil {- env.Name = *input.Body.Name- }- if input.Body.Enabled != nil {- env.Enabled = *input.Body.Enabled- }- if input.Body.IsEdge != nil {- env.IsEdge = *input.Body.IsEdge- }- useApiKey := input.Body.UseApiKey != nil && *input.Body.UseApiKey- if useApiKey {- env.Status = string(EnvironmentStatusPending)- } else if input.Body.AccessToken != nil && *input.Body.AccessToken != "" {- env.AccessToken = input.Body.AccessToken- }-- created, err := h.environmentService.CreateEnvironment(ctx, env, &user.ID, &user.Username)+ created, err := h.environmentService.CreateEnvironment(ctx, &Environment{+ Name: kit.FromPtr(input.Body.Name),+ ApiUrl: input.Body.ApiUrl,+ Status: kit.Ternary(kit.FromPtr(input.Body.UseApiKey), string(EnvironmentStatusPending), ""),+ Enabled: mo.PointerToOption(input.Body.Enabled).OrElse(true),+ IsEdge: kit.FromPtr(input.Body.IsEdge),+ AccessToken: kit.Ternary(!kit.FromPtr(input.Body.UseApiKey) && kit.FromPtr(input.Body.AccessToken) != "", input.Body.AccessToken, nil),+ }, &user.ID, &user.Username)if err != nil {return nil, huma.Error500InternalServerError("Failed to create environment: " + err.Error())}- var apiKey *string- if useApiKey {+ created, apiKey, err := func() (*Environment, *string, error) {+ if !kit.FromPtr(input.Body.UseApiKey) {+ if kit.FromPtr(created.AccessToken) != "" {+ h.triggerEnvironmentResourceSync(ctx, created.ID, created.Name, "environment creation")+ }+ return created, nil, nil+ }+apiKeyDto, keyErr := h.apiKeyService.CreateEnvironmentApiKey(ctx, created.ID)if keyErr != nil {slog.ErrorContext(ctx, "Failed to create environment API key", "environmentId", created.ID, "error", keyErr.Error())- return nil, huma.Error500InternalServerError("Failed to create environment API key")+ return nil, nil, huma.Error500InternalServerError("Failed to create environment API key")}// The full key doubles as the manager-to-agent access token.- updates := map[string]any{+ updated, updateErr := h.environmentService.UpdateEnvironment(ctx, created.ID, map[string]any{"api_key_id": apiKeyDto.ID,"access_token": apiKeyDto.Key,- }- updated, updateErr := h.environmentService.UpdateEnvironment(ctx, created.ID, updates, &user.ID, &user.Username)+ }, &user.ID, &user.Username)if updateErr != nil {// ErrApiKeyProtected means the link landed, so the key belongs to the environment and must survive.if delErr := h.apiKeyService.DeleteApiKey(ctx, apiKeyDto.ID); delErr != nil &&@@ -347,24 +339,23 @@ func (h *EnvironmentHandler) CreateEnvironment(ctx context.Context, input *Creatslog.ErrorContext(ctx, "Failed to clean up unlinked environment API key", "environmentId", created.ID, "error", delErr.Error())}slog.ErrorContext(ctx, "Failed to link API key to environment", "environmentId", created.ID, "error", updateErr.Error())- return nil, huma.Error500InternalServerError("Failed to link API key")+ return nil, nil, huma.Error500InternalServerError("Failed to link API key")}- created = updated- apiKey = new(apiKeyDto.Key)- } else if created.AccessToken != nil && *created.AccessToken != "" {- h.triggerEnvironmentResourceSync(ctx, created.ID, created.Name, "environment creation")+ return updated, new(apiKeyDto.Key), nil+ }()+ if err != nil {+ return nil, err}out, mapErr := mapping.MapOne[*Environment, environment.Environment](created)if mapErr != nil {return nil, huma.Error500InternalServerError("Failed to map environment")}- ApplyEnvironmentRuntimeState(&out)return &handlerutil.Out[EnvironmentWithApiKey]{Body: base.ApiResponse[EnvironmentWithApiKey]{Success: true,- Data: EnvironmentWithApiKey{Environment: out, ApiKey: apiKey},+ Data: EnvironmentWithApiKey{Environment: ApplyEnvironmentRuntimeState(out), ApiKey: apiKey},},}, nil}@@ -380,11 +371,41 @@ func (h *EnvironmentHandler) GetEnvironment(ctx context.Context, input *GetEnvirif mapErr != nil {return nil, huma.Error500InternalServerError("Failed to map environment")}- ApplyEnvironmentRuntimeState(&out)+ out = ApplyEnvironmentRuntimeState(out)if env.IsEdge {- if certInfo, certErr := readGeneratedEdgeMTLSCertificateInfo(h.edgeMTLSConfig(), env.ID); certErr == nil {- out.EdgeMTLSCertificate = certInfo- }+ out.EdgeMTLSCertificate = func() *environment.EdgeMTLSCertificate { // keep: optional field on a 22-field mapped DTO+ edgeCfg := h.edgeMTLSConfig()+ if edge.NormalizeEdgeMTLSMode(edgeCfg.EdgeMTLSMode) == edge.EdgeMTLSModeDisabled {+ return nil+ }+ certPath, pathErr := edge.GeneratedManagerClientMTLSCertPath(edgeCfg, env.ID)+ if pathErr != nil {+ return nil+ }+ // os rather than acfs: the assets dir may be configured anywhere on the host.+ certPEM, readErr := os.ReadFile(certPath)+ if readErr != nil {+ return nil+ }+ block, _ := pem.Decode(certPEM)+ if block == nil {+ return nil+ }+ cert, parseErr := x509.ParseCertificate(block.Bytes)+ if parseErr != nil {+ return nil+ }++ expiresAt := cert.NotAfter.UTC()+ now := time.Now().UTC()+ return &environment.EdgeMTLSCertificate{+ CommonName: kit.Ternary(strings.TrimSpace(cert.Subject.CommonName) != "", new(strings.TrimSpace(cert.Subject.CommonName)), nil),+ ExpiresAt: &expiresAt,+ DaysRemaining: new(int(math.Ceil(max(expiresAt.Sub(now), 0).Hours() / 24))),+ Expired: now.After(expiresAt),+ ExpiringSoon: now.Before(expiresAt) && expiresAt.Sub(now) <= edgeMTLSCertificateExpiryWarningWindow,+ }+ }()}return &handlerutil.Out[environment.Environment]{@@ -397,21 +418,20 @@ func (h *EnvironmentHandler) GetEnvironment(ctx context.Context, input *GetEnvir// UpdateEnvironment updates an environment.func (h *EnvironmentHandler) UpdateEnvironment(ctx context.Context, input *UpdateEnvironmentInput) (*handlerutil.Out[environment.Environment], error) {- req := &input.Bodyupdates := map[string]any{}if input.ID != LocalEnvironmentID {- if req.ApiUrl != nil {- updates["api_url"] = *req.ApiUrl+ if input.Body.ApiUrl != nil {+ updates["api_url"] = *input.Body.ApiUrl}- if req.Enabled != nil {- updates["enabled"] = *req.Enabled+ if input.Body.Enabled != nil {+ updates["enabled"] = *input.Body.Enabled}- if req.AccessToken != nil {- updates["access_token"] = *req.AccessToken+ if input.Body.AccessToken != nil {+ updates["access_token"] = *input.Body.AccessToken}}- if req.Name != nil {- updates["name"] = *req.Name+ if input.Body.Name != nil {+ updates["name"] = *input.Body.Name}userID, username := currentUserRefs(ctx)@@ -433,37 +453,42 @@ func (h *EnvironmentHandler) UpdateEnvironment(ctx context.Context, input *Updat}}(context.WithoutCancel(ctx), input.ID, updated.Name)}- if updated.AccessToken != nil && *updated.AccessToken != "" && ((req.AccessToken != nil && *req.AccessToken != "") || req.Name != nil) {+ if kit.FromPtr(updated.AccessToken) != "" && (kit.FromPtr(input.Body.AccessToken) != "" || input.Body.Name != nil) {h.triggerEnvironmentResourceSync(ctx, input.ID, updated.Name, "environment update")}- var newApiKey *string- if req.RegenerateApiKey != nil && *req.RegenerateApiKey {+ updated, newApiKey, err := func() (*Environment, *string, error) {+ if !kit.FromPtr(input.Body.RegenerateApiKey) {+ return updated, nil, nil+ }localUser, err := handlerutil.RequireUser(ctx)if err != nil {- return nil, err+ return nil, nil, err}apiKey, err := h.environmentService.RegenerateEnvironmentApiKey(ctx, updated, localUser.ID, localUser.Username)if err != nil {slog.ErrorContext(ctx, "Failed to regenerate API key", "environmentId", input.ID, "error", err.Error())- return nil, huma.Error500InternalServerError("Failed to regenerate API key")+ return nil, nil, huma.Error500InternalServerError("Failed to regenerate API key")}- updated, err = h.environmentService.GetEnvironmentByID(ctx, input.ID)+ refreshed, err := h.environmentService.GetEnvironmentByID(ctx, input.ID)if err != nil {slog.ErrorContext(ctx, "Failed to fetch updated environment", "environmentId", input.ID, "error", err.Error())- return nil, huma.Error500InternalServerError("Failed to fetch updated environment")+ return nil, nil, huma.Error500InternalServerError("Failed to fetch updated environment")}- newApiKey = new(apiKey)+ return refreshed, new(apiKey), nil+ }()+ if err != nil {+ return nil, err}out, mapErr := mapping.MapOne[*Environment, environment.Environment](updated)if mapErr != nil {return nil, huma.Error500InternalServerError("Failed to map environment")}- ApplyEnvironmentRuntimeState(&out)- out.ApiKey = newApiKey+ out = ApplyEnvironmentRuntimeState(out)+ out.ApiKey = newApiKey // keep: optional field on a 22-field mapped DTOreturn &handlerutil.Out[environment.Environment]{Body: base.ApiResponse[environment.Environment]{@@ -497,30 +522,28 @@ func (h *EnvironmentHandler) DeleteEnvironment(ctx context.Context, input *Delet// TestConnection tests connectivity to an environment.func (h *EnvironmentHandler) TestConnection(ctx context.Context, input *TestConnectionInput) (*handlerutil.Out[environment.Test], error) {- var apiUrl *string- if input.Body != nil {- apiUrl = input.Body.ApiUrl- }- if apiUrl != nil {+ if kit.FromPtr(input.Body).ApiUrl != nil {permissions, ok := middleware.PermissionsFromContext(ctx)if !ok || !permissions.Allows(authz.PermEnvironmentsUpdate, "") {return nil, huma.Error403Forbidden("permission denied: " + authz.PermEnvironmentsUpdate)}}- status, err := h.environmentService.TestConnection(ctx, input.ID, apiUrl)- resp := environment.Test{Status: status}+ status, err := h.environmentService.TestConnection(ctx, input.ID, kit.FromPtr(input.Body).ApiUrl)+ if err != nil && kit.FromPtr(input.Body).ApiUrl != nil {+ apiErr := common.ToAPIError(err)+ return &handlerutil.Out[environment.Test]{+ Body: base.ApiResponse[environment.Test]{+ Success: false,+ Data: environment.Test{Status: status},+ },+ }, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)+ }if err != nil {- if apiUrl == nil {- resp.Message = new(err.Error())- } else {- apiErr := common.ToAPIError(err)- err = huma.NewError(apiErr.HTTPStatus(), apiErr.Message)- }return &handlerutil.Out[environment.Test]{Body: base.ApiResponse[environment.Test]{Success: false,- Data: resp,+ Data: environment.Test{Status: status, Message: new(err.Error())},},}, err}@@ -528,7 +551,7 @@ func (h *EnvironmentHandler) TestConnection(ctx context.Context, input *TestConnreturn &handlerutil.Out[environment.Test]{Body: base.ApiResponse[environment.Test]{Success: true,- Data: resp,+ Data: environment.Test{Status: status},},}, nil}@@ -548,9 +571,8 @@ func (h *EnvironmentHandler) PairAgent(ctx context.Context, input *PairAgentInpureturn nil, huma.Error404NotFound("Not found")}- shouldRotate := input.Body != nil && input.Body.Rotate != nil && *input.Body.Rotate- if h.cfg.AgentToken == "" || shouldRotate {- h.cfg.AgentToken = kit.RandomString(48)+ if h.cfg.AgentToken == "" || kit.FromPtr(kit.FromPtr(input.Body).Rotate) {+ h.cfg.AgentToken = kit.RandomString(48) // keep: rotates the shared runtime config in place}if err := h.settingsService.SetStringSetting(ctx, "agentToken", h.cfg.AgentToken); err != nil {@@ -651,54 +673,50 @@ func (h *EnvironmentHandler) GetDeploymentSnippets(ctx context.Context, input *Greturn nil, huma.Error400BadRequest("Environment does not have an API key configured")}- if env.AccessToken == nil || *env.AccessToken == "" {+ if kit.FromPtr(env.AccessToken) == "" {return nil, huma.Error400BadRequest("Environment is missing access token")}- var snippets *DeploymentSnippets- if env.IsEdge {- snippets, err = h.environmentService.GenerateEdgeDeploymentSnippets(ctx, env.ID, h.cfg.GetAppURL(), *env.AccessToken, h.edgeMTLSConfig())- } else {- snippets, err = h.environmentService.GenerateDeploymentSnippets(ctx, env.ID, h.cfg.GetAppURL(), env.ApiUrl, *env.AccessToken)- }+ snippets, err := func() (*DeploymentSnippets, error) {+ if env.IsEdge {+ return h.environmentService.GenerateEdgeDeploymentSnippets(ctx, env.ID, h.cfg.GetAppURL(), *env.AccessToken, h.edgeMTLSConfig())+ }+ return h.environmentService.GenerateDeploymentSnippets(ctx, env.ID, h.cfg.GetAppURL(), env.ApiUrl, *env.AccessToken)+ }()if err != nil {slog.ErrorContext(ctx, "Failed to generate deployment snippets", "environmentId", input.ID, "error", err.Error())return nil, huma.Error500InternalServerError("Failed to generate deployment snippets")}- var mtls *DeploymentSnippetMTLS- if snippets.MTLS != nil {- files := make([]DeploymentSnippetFile, 0, len(snippets.MTLS.Files))- for _, file := range snippets.MTLS.Files {- entry := DeploymentSnippetFile{- Name: file.Name,- ContainerPath: file.ContainerPath,- Permissions: file.Permissions,- DownloadURL: fmt.Sprintf("/api/environments/%s/deployment/mtls/%s", env.ID, file.Name),- }- // Secret material is only served through the download endpoint.- if isSensitiveMTLSAssetName(file.Name) {- entry.Sensitive = true- } else {- entry.Content = file.Content- }- files = append(files, entry)- }- mtls = &DeploymentSnippetMTLS{- DockerRun: snippets.MTLS.DockerRun,- DockerCompose: snippets.MTLS.DockerCompose,- Files: files,- HostDirHint: snippets.MTLS.HostDirHint,- }- }-return &handlerutil.Out[DeploymentSnippet]{Body: base.ApiResponse[DeploymentSnippet]{Success: true,Data: DeploymentSnippet{DockerRun: snippets.DockerRun,DockerCompose: snippets.DockerCompose,- MTLS: mtls,+ MTLS: func() *DeploymentSnippetMTLS {+ if snippets.MTLS == nil {+ return nil+ }+ files := make([]DeploymentSnippetFile, 0, len(snippets.MTLS.Files))+ for _, file := range snippets.MTLS.Files {+ // Secret material is only served through the download endpoint.+ files = append(files, DeploymentSnippetFile{+ Name: file.Name,+ Content: kit.Ternary(isSensitiveMTLSAssetName(file.Name), "", file.Content),+ DownloadURL: fmt.Sprintf("/api/environments/%s/deployment/mtls/%s", env.ID, file.Name),+ Sensitive: isSensitiveMTLSAssetName(file.Name),+ ContainerPath: file.ContainerPath,+ Permissions: file.Permissions,+ })+ }+ return &DeploymentSnippetMTLS{+ DockerRun: snippets.MTLS.DockerRun,+ DockerCompose: snippets.MTLS.DockerCompose,+ Files: files,+ HostDirHint: snippets.MTLS.HostDirHint,+ }+ }(),},},}, nil@@ -715,7 +733,7 @@ func (h *EnvironmentHandler) GetEnvironmentVersion(ctx context.Context, input *Gdefer cancel()var versionInfo version.Info- if env.ID == LocalEnvironmentID && h.localVersion != nil {+ if env.ID == LocalEnvironmentID {versionInfo = *h.localVersion(reqCtx)} else if proxyErr := h.environmentService.ProxyJSONRequestForEnvironment(reqCtx, *env, http.MethodGet, "/api/app-version", nil, &versionInfo); proxyErr != nil {return nil, handlerutil.TranslateRemoteProxyError(proxyErr)@@ -769,11 +787,12 @@ func (h *EnvironmentHandler) DownloadEnvironmentMTLSBundle(ctx context.Context,Name: downloadName,Method: zip.Deflate,}- mode := kit.Ternary[os.FileMode](isSensitiveMTLSAssetName(file.Name), 0o600, 0o644)- if parsed, parseErr := strconv.ParseUint(strings.TrimSpace(file.Permissions), 8, 32); parseErr == nil && parsed != 0 {- mode = os.FileMode(parsed)- }- header.SetMode(mode)+ header.SetMode(func() os.FileMode {+ if parsed, parseErr := strconv.ParseUint(strings.TrimSpace(file.Permissions), 8, 32); parseErr == nil && parsed != 0 {+ return os.FileMode(parsed)+ }+ return kit.Ternary[os.FileMode](isSensitiveMTLSAssetName(file.Name), 0o600, 0o644)+ }())entry, createErr := zipWriter.CreateHeader(header)if createErr != nil {@@ -816,9 +835,6 @@ func (h *EnvironmentHandler) DownloadEnvironmentMTLSFile(ctx context.Context, in// edgeMTLSConfig maps the handler config to edge mTLS settings, or nil without config.func (h *EnvironmentHandler) edgeMTLSConfig() *edge.Config {- if h.cfg == nil {- return nil- }return &edge.Config{EdgeMTLSMode: h.cfg.EdgeMTLSMode,EdgeMTLSCAFile: h.cfg.EdgeMTLSCAFile,@@ -838,7 +854,7 @@ func (h *EnvironmentHandler) loadEnvironmentMTLSFiles(ctx context.Context, envirif env.ApiKeyID == nil {return nil, nil, huma.Error400BadRequest("Environment does not have an API key configured")}- if env.AccessToken == nil || *env.AccessToken == "" {+ if kit.FromPtr(env.AccessToken) == "" {return nil, nil, huma.Error400BadRequest("Environment is missing access token")}@@ -869,31 +885,25 @@ func (h *EnvironmentHandler) mtlsDownloadResponse(env *Environment, contentType,slog.WarnContext(ctx, "Failed to stream edge mTLS download", "fileName", fileName, "bytesWritten", written, "bytesExpected", len(content), "error", writeErr)return}- if h.eventService == nil {- return- }if remoteAddr := strings.TrimSpace(middleware.GetRemoteAddrFromContext(ctx)); remoteAddr != "" {metadata["remoteAddr"] = remoteAddr}userID, username := currentUserRefs(ctx)- req := event.CreateEventRequest{- Type: event.EventTypeEnvironmentMTLSDownload,- Severity: event.EventSeverityInfo,- Title: title,- Description: description,- UserID: userID,- Username: username,- Metadata: metadata,- }- if env != nil {- req.ResourceType = new("environment")- req.ResourceID = new(env.ID)- req.ResourceName = new(env.Name)- req.EnvironmentID = new(env.ID)- }- if _, err := h.eventService.CreateEvent(ctx, req); err != nil {- slog.WarnContext(ctx, "Failed to record mTLS audit event", "type", string(req.Type), "error", err)+ if _, err := h.eventService.CreateEvent(ctx, event.CreateEventRequest{+ Type: event.EventTypeEnvironmentMTLSDownload,+ Severity: event.EventSeverityInfo,+ Title: title,+ Description: description,+ ResourceType: kit.Ternary(env != nil, new("environment"), nil),+ ResourceID: kit.Ternary(env != nil, new(kit.FromPtr(env).ID), nil),+ ResourceName: kit.Ternary(env != nil, new(kit.FromPtr(env).Name), nil),+ UserID: userID,+ Username: username,+ EnvironmentID: kit.Ternary(env != nil, new(kit.FromPtr(env).ID), nil),+ Metadata: metadata,+ }); err != nil {+ slog.WarnContext(ctx, "Failed to record mTLS audit event", "type", string(event.EventTypeEnvironmentMTLSDownload), "error", err)}},}@@ -930,48 +940,3 @@ func environmentMTLSAssetDownloadName(env *Environment, fileName string) string}const edgeMTLSCertificateExpiryWarningWindow = 30 * 24 * time.Hour--func readGeneratedEdgeMTLSCertificateInfo(edgeCfg *edge.Config, envID string) (*environment.EdgeMTLSCertificate, error) {- if edgeCfg == nil {- return nil, errors.New("config not available")- }- if edge.NormalizeEdgeMTLSMode(edgeCfg.EdgeMTLSMode) == edge.EdgeMTLSModeDisabled {- return nil, errors.New("edge mTLS is disabled")- }-- certPath, err := edge.GeneratedManagerClientMTLSCertPath(edgeCfg, envID)- if err != nil {- return nil, fmt.Errorf("resolve generated edge mTLS client certificate path: %w", err)- }- // os rather than acfs: the assets dir may be configured anywhere on the host.- certPEM, err := os.ReadFile(certPath)- if err != nil {- return nil, fmt.Errorf("read generated edge mTLS client certificate: %w", err)- }-- block, _ := pem.Decode(certPEM)- if block == nil {- return nil, errors.New("decode generated edge mTLS client certificate PEM")- }-- cert, err := x509.ParseCertificate(block.Bytes)- if err != nil {- return nil, fmt.Errorf("parse generated edge mTLS client certificate: %w", err)- }-- expiresAt := cert.NotAfter.UTC()- now := time.Now().UTC()- remaining := expiresAt.Sub(now)- info := &environment.EdgeMTLSCertificate{- ExpiresAt: &expiresAt,- DaysRemaining: new(int(math.Ceil(max(remaining, 0).Hours() / 24))),- Expired: now.After(expiresAt),- ExpiringSoon: now.Before(expiresAt) && remaining <= edgeMTLSCertificateExpiryWarningWindow,- }-- if commonName := strings.TrimSpace(cert.Subject.CommonName); commonName != "" {- info.CommonName = &commonName- }-- return info, nil-}diff --git a/backend/internal/environment/helpers.go b/backend/internal/environment/helpers.goindex 11336bde9..5086ed58a 100644--- a/backend/internal/environment/helpers.go+++ b/backend/internal/environment/helpers.go@@ -6,7 +6,6 @@ import ("sync""github.com/samber/hot"- "github.com/samber/mo")// edgeTokenCache maps agent tokens to environment IDs; the reverse index drops@@ -46,68 +45,22 @@ type runtimeWatchers struct {seq int}-func (c *edgeTokenCache) environmentID(token string) mo.Option[string] {- if c == nil || c.byToken == nil || token == "" {- return mo.None[string]()- }-- staleEnvironmentID, wasCached := c.byToken.Peek(token)- environmentID, ok, _ := c.byToken.Get(token)- if ok {- return mo.Some(environmentID)- }- if wasCached {- c.mu.Lock()- if currentToken, indexed := c.byEnvID[staleEnvironmentID]; indexed && currentToken == token {- delete(c.byEnvID, staleEnvironmentID)- }- c.mu.Unlock()- }- return mo.None[string]()-}--func (c *edgeTokenCache) put(envID, token string) {- if c == nil || c.byToken == nil || envID == "" || token == "" {- return- }-- c.mu.Lock()- defer c.mu.Unlock()-- if previousToken, ok := c.byEnvID[envID]; ok && previousToken != token {- c.byToken.Delete(previousToken)- }-- c.byEnvID[envID] = token- c.byToken.Set(token, envID)-}--func (c *edgeTokenCache) invalidate(envID string) {- if c == nil || envID == "" {+// sync replaces an environment's cached token, dropping it when the new token is blank.+func (c *edgeTokenCache) sync(envID, token string) {+ if envID == "" {return}c.mu.Lock()defer c.mu.Unlock()- if token, ok := c.byEnvID[envID]; ok {+ if previousToken, ok := c.byEnvID[envID]; ok {delete(c.byEnvID, envID)- if c.byToken != nil {- c.byToken.Delete(token)- }- }-}--// sync replaces an environment's cached token, dropping it when the new token is blank.-func (c *edgeTokenCache) sync(envID, token string) {- if c == nil || envID == "" {- return+ c.byToken.Delete(previousToken)}-- c.invalidate(envID)-if resolvedToken := strings.TrimSpace(token); resolvedToken != "" {- c.put(envID, resolvedToken)+ c.byEnvID[envID] = resolvedToken+ c.byToken.Set(resolvedToken, envID)}}@@ -115,104 +68,18 @@ func isActiveRemoteEnvironment(env Environment) bool {return env.ID != "" && env.ID != "0" && env.Enabled && !env.Hidden}-func (c *remoteEnvSnapshotCache) get(environmentID string) mo.Option[Environment] {- if c == nil || environmentID == "" {- return mo.None[Environment]()- }-- c.mu.RLock()- envRecord, ok := c.envs[environmentID]- c.mu.RUnlock()- if !ok || !isActiveRemoteEnvironment(envRecord) {- return mo.None[Environment]()- }- return mo.Some(envRecord)-}-func (c *remoteEnvSnapshotCache) put(env Environment) {- if c == nil {- return- }-- if !isActiveRemoteEnvironment(env) {- c.remove(env.ID)- return- }-- c.mu.Lock()- c.envs[env.ID] = env- c.revision++- c.mu.Unlock()-}--func (c *remoteEnvSnapshotCache) remove(environmentID string) {- if c == nil || environmentID == "" {- return- }-- c.mu.Lock()- delete(c.envs, environmentID)- c.revision++- c.mu.Unlock()-}--func (c *remoteEnvSnapshotCache) update(environmentID string, update func(*Environment)) {- if c == nil || environmentID == "" || update == nil {+ if env.ID == "" {return}c.mu.Lock()defer c.mu.Unlock()- c.revision++- envRecord, ok := c.envs[environmentID]- if !ok {- return- }- update(&envRecord)- if isActiveRemoteEnvironment(envRecord) {- c.envs[environmentID] = envRecord+ if isActiveRemoteEnvironment(env) {+ c.envs[env.ID] = env} else {- delete(c.envs, environmentID)- }-}--// subscribe returns a never-closed channel that receives a coalesced wake-up on-// liveness changes, plus a function to release it.-func (w *runtimeWatchers) subscribe() (<-chan struct{}, func()) {- w.mu.Lock()- defer w.mu.Unlock()-- if w.chans == nil {- w.chans = make(map[int]chan struct{})- }-- w.seq++- id := w.seq- // Capacity 1: watchers read live state, so a second pending signal is redundant.- ch := make(chan struct{}, 1)- w.chans[id] = ch-- return ch, func() {- w.mu.Lock()- defer w.mu.Unlock()- delete(w.chans, id)- }-}--// notify wakes every watcher without blocking, so tunnel callbacks may call it.-func (w *runtimeWatchers) notify() {- if w == nil {- return- }-- w.mu.Lock()- defer w.mu.Unlock()-- for _, ch := range w.chans {- select {- case ch <- struct{}{}:- default:- }+ delete(c.envs, env.ID)}+ c.revision++}diff --git a/backend/internal/environment/module.go b/backend/internal/environment/module.goindex 50e15fe1d..b9ee842fc 100644--- a/backend/internal/environment/module.go+++ b/backend/internal/environment/module.go@@ -57,8 +57,17 @@ func (m *Module) RegisterRoutes(api huma.API, appCtx handlerutil.ActivityAppContRegisterEnvironments(api, NewHandler(nil, nil, nil, nil, nil, nil))return}- m.handler.appCtx = appCtx.Context()- m.handler.localVersion = localVersion+ *m.handler = EnvironmentHandler{ //exhaustruct:enforce+ environmentService: m.handler.environmentService,+ settingsService: m.handler.settingsService,+ apiKeyService: m.handler.apiKeyService,+ eventService: m.handler.eventService,+ cfg: m.handler.cfg,+ streamHub: m.handler.streamHub,+ activityService: m.handler.activityService,+ appCtx: appCtx.Context(),+ localVersion: localVersion,+ }RegisterEnvironments(api, m.handler)}diff --git a/backend/internal/environment/service.go b/backend/internal/environment/service.goindex 68be261f5..64680cfcf 100644--- a/backend/internal/environment/service.go+++ b/backend/internal/environment/service.go@@ -76,7 +76,7 @@ type EnvironmentService struct {runtimeWatchers runtimeWatchers// syncGate skips agent config pushes while the last accepted payload is unchanged.- syncGate utils.SyncGate+ syncGate *utils.SyncGateproxy *proxy.Servicehealth *health.Service@@ -106,6 +106,34 @@ type VariableSyncer interface {ForgetSyncState(envID string)}+type resourceSync struct {+ kind string+ sync func(ctx context.Context, environmentID string) error+}++// DeploymentSnippets contains deployment configuration snippets for an environment.+type DeploymentSnippets struct {+ DockerRun string+ DockerCompose string+ MTLS *DeploymentSnippetMTLS+}++type DeploymentSnippetFile struct {+ Name string `json:"name" doc:"Suggested filename"`+ Content string `json:"content,omitempty" doc:"PEM file contents. Omitted for sensitive files such as private keys; use downloadUrl instead."`+ DownloadURL string `json:"downloadUrl,omitempty" doc:"Pairing-permission endpoint to download this file when content is withheld"`+ Sensitive bool `json:"sensitive,omitempty" doc:"True when this file is sensitive and must be fetched via downloadUrl"`+ ContainerPath string `json:"containerPath" doc:"Container mount path expected by the mTLS snippet"`+ Permissions string `json:"permissions" doc:"Suggested file mode"`+}++type DeploymentSnippetMTLS struct {+ DockerRun string `json:"dockerRun" doc:"Docker run snippet using Arcane-generated mTLS assets"`+ DockerCompose string `json:"dockerCompose" doc:"Docker compose snippet using Arcane-generated mTLS assets"`+ Files []DeploymentSnippetFile `json:"files" doc:"Generated PEM files to place on the edge host"`+ HostDirHint string `json:"hostDirHint" doc:"Suggested host directory containing the generated PEM files"`+}+// ForgetSyncState makes the next sync of every resource group resend regardless of payload changes.func (s *EnvironmentService) ForgetSyncState(environmentID string) {s.syncGate.Forget(environmentID)@@ -122,11 +150,9 @@ func NewEnvironmentService(settingsService *settings.SettingsService,apiKeyService *apikey.ApiKeyService,) *EnvironmentService {- if httpClient == nil {- httpClient = http.DefaultClient- }- s := &EnvironmentService{- syncGate: utils.SyncGate{Expiry: SyncDeliveryExpiry},+ syncGate := &utils.SyncGate{Expiry: SyncDeliveryExpiry}+ return &EnvironmentService{+ syncGate: syncGate,db: db,eventService: eventService,settingsService: settingsService,@@ -141,14 +167,13 @@ func NewEnvironmentService(WithMissingSharedCache().WithJanitor().Build(),- jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScope),- health: health.New(httpClient, dockerService),- snippets: snippets.New(eventService),- swarm: swarm.New(apiKeyService),+ jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScope),+ runtimeWatchers: runtimeWatchers{chans: make(map[int]chan struct{})},+ proxy: proxy.New(db, httpClient, settingsService, syncGate),+ health: health.New(httpClient, dockerService),+ snippets: snippets.New(eventService),+ swarm: swarm.New(apiKeyService),}- s.proxy = proxy.New(db, httpClient, settingsService, &s.syncGate)-- return s}// RegisterMetrics reports the arcane.environments gauge; only the manager calls it.@@ -204,9 +229,17 @@ func (s *EnvironmentService) ResolveEdgeEnvironmentByToken(ctx context.Context,return "", errors.New("agent token required")}- if envID, ok := s.edgeTokens.environmentID(token).Get(); ok {+ staleEnvironmentID, wasCached := s.edgeTokens.byToken.Peek(token)+ if envID, ok, _ := s.edgeTokens.byToken.Get(token); ok {return envID, nil}+ if wasCached {+ s.edgeTokens.mu.Lock()+ if currentToken, indexed := s.edgeTokens.byEnvID[staleEnvironmentID]; indexed && currentToken == token {+ delete(s.edgeTokens.byEnvID, staleEnvironmentID)+ }+ s.edgeTokens.mu.Unlock()+ }var env Environmentif err := s.db.WithContext(ctx).@@ -214,46 +247,39 @@ func (s *EnvironmentService) ResolveEdgeEnvironmentByToken(ctx context.Context,Where("is_edge = ?", true).Where("access_token = ?", token).First(&env).Error; err != nil {- if errors.Is(err, gorm.ErrRecordNotFound) {- s.logEdgeTokenResolveMiss(ctx, token)- return "", errors.New("invalid agent token")+ if !errors.Is(err, gorm.ErrRecordNotFound) {+ return "", fmt.Errorf("failed to resolve edge environment by token: %w", err)+ }+ // Log edge environment counts so a miss can be diagnosed; only the token's+ // length and irreversible fingerprint are logged.+ if slog.Default().Enabled(ctx, slog.LevelDebug) {+ var totalEdgeEnvs int64+ var edgeEnvsWithToken int64+ totalEdgeEnvsErr := s.db.WithContext(ctx).Model(&Environment{}).Where("is_edge = ?", true).Count(&totalEdgeEnvs).Error+ edgeEnvsWithTokenErr := s.db.WithContext(ctx).Model(&Environment{}).+ Where("is_edge = ?", true).+ Where("access_token IS NOT NULL AND access_token != ?", "").+ Count(&edgeEnvsWithToken).Error++ args := []any{+ "token_length", len(token),+ "token_fingerprint", remenv.RedactedTokenFingerprint(token),+ }+ if totalEdgeEnvsErr == nil {+ args = append(args, "edge_envs_total", totalEdgeEnvs)+ }+ if edgeEnvsWithTokenErr == nil {+ args = append(args, "edge_envs_with_access_token", edgeEnvsWithToken)+ }+ slog.DebugContext(ctx, "Edge agent token did not match any environment", args...)}- return "", fmt.Errorf("failed to resolve edge environment by token: %w", err)+ return "", errors.New("invalid agent token")}- s.edgeTokens.put(env.ID, token)+ s.edgeTokens.sync(env.ID, token)return env.ID, nil}-// logEdgeTokenResolveMiss logs edge environment counts at debug level so a token-// miss can be diagnosed; only the token's length and irreversible fingerprint are logged.-func (s *EnvironmentService) logEdgeTokenResolveMiss(ctx context.Context, token string) {- if !slog.Default().Enabled(ctx, slog.LevelDebug) {- return- }-- var totalEdgeEnvs int64- var edgeEnvsWithToken int64- totalEdgeEnvsErr := s.db.WithContext(ctx).Model(&Environment{}).Where("is_edge = ?", true).Count(&totalEdgeEnvs).Error- edgeEnvsWithTokenErr := s.db.WithContext(ctx).Model(&Environment{}).- Where("is_edge = ?", true).- Where("access_token IS NOT NULL AND access_token != ?", "").- Count(&edgeEnvsWithToken).Error-- args := []any{- "token_length", len(token),- "token_fingerprint", remenv.RedactedTokenFingerprint(token),- }- if totalEdgeEnvsErr == nil {- args = append(args, "edge_envs_total", totalEdgeEnvs)- }- if edgeEnvsWithTokenErr == nil {- args = append(args, "edge_envs_with_access_token", edgeEnvsWithToken)- }-- slog.DebugContext(ctx, "Edge agent token did not match any environment", args...)-}-// ResolveEnvironmentName returns an environment's display label; names are// user-editable, so never hardcode one for a known ID.func (s *EnvironmentService) ResolveEnvironmentName(ctx context.Context, environmentID string) string {@@ -261,7 +287,7 @@ func (s *EnvironmentService) ResolveEnvironmentName(ctx context.Context, environenvironmentID = LocalEnvironmentID}env, err := s.GetEnvironmentByIDCached(ctx, environmentID)- if err != nil || env == nil {+ if err != nil {if !errors.Is(err, context.Canceled) {slog.WarnContext(ctx, "failed to resolve environment name", "environmentId", environmentID, "error", err)}@@ -290,7 +316,7 @@ func (s *EnvironmentService) EnsureLocalEnvironment(ctx context.Context, appUrl}now := time.Now()- localEnv := &Environment{+ if createLocalEnvironmentErr := s.db.WithContext(ctx).Create(&Environment{ID: LocalEnvironmentID,CreatedAt: now,UpdatedAt: new(now),@@ -298,9 +324,7 @@ func (s *EnvironmentService) EnsureLocalEnvironment(ctx context.Context, appUrlApiUrl: appUrl,Status: string(EnvironmentStatusOnline),Enabled: true,- }-- if createLocalEnvironmentErr := s.db.WithContext(ctx).Create(localEnv).Error; createLocalEnvironmentErr != nil {+ }).Error; createLocalEnvironmentErr != nil {return fmt.Errorf("failed to create local environment: %w", createLocalEnvironmentErr)}s.invalidateEnvironmentCache(LocalEnvironmentID)@@ -313,16 +337,27 @@ func (s *EnvironmentService) CreateEnvironment(ctx context.Context, env *Environif err := normalization.Normalize(env); err != nil {return nil, err}- env.ID = uuid.New().String()-- // Only set status to offline if not already set (e.g., API key flow sets it to pending)- if env.Status == "" {- env.Status = string(EnvironmentStatusOffline)- }-now := time.Now()- env.CreatedAt = now- env.UpdatedAt = new(now)+ *env = Environment{ //exhaustruct:enforce+ BaseModel: database.BaseModel{+ ID: uuid.New().String(),+ CreatedAt: now,+ UpdatedAt: new(now),+ },+ Name: env.Name,+ ApiUrl: env.ApiUrl,+ // Only set status to offline if not already set (e.g., API key flow sets it to pending)+ Status: cmp.Or(env.Status, string(EnvironmentStatusOffline)),+ Enabled: env.Enabled,+ IsEdge: env.IsEdge,+ Hidden: env.Hidden,+ LastSeen: env.LastSeen,+ LastEdgeTransport: env.LastEdgeTransport,+ AccessToken: env.AccessToken,+ ApiKeyID: env.ApiKeyID,+ ParentEnvironmentID: env.ParentEnvironmentID,+ SwarmNodeID: env.SwarmNodeID,+ }if err := s.db.WithContext(ctx).Create(env).Error; err != nil {return nil, fmt.Errorf("failed to create environment: %w", err)@@ -361,17 +396,15 @@ func (s *EnvironmentService) UpdateEnvironment(ctx context.Context, id string, ureturn nil, err}- var nextAPIURL *string- if rawAPIURL, ok := updates["api_url"]; ok {- if apiURL, isString := rawAPIURL.(string); isString {- nextAPIURL = new(apiURL)- }- }- _, accessTokenUpdated := updates["access_token"]if validateCredentialTargetChangeErr := validation.ValidateCredentialTargetChange("environment API URL",current.ApiUrl,- nextAPIURL,+ func() *string {+ if apiURL, isString := updates["api_url"].(string); isString {+ return new(apiURL)+ }+ return nil+ }(),func(value string) string {normalized, normalizeErr := httpx.NormalizeBaseURL(value)if normalizeErr != nil {@@ -379,8 +412,11 @@ func (s *EnvironmentService) UpdateEnvironment(ctx context.Context, id string, u}return normalized},- map[string]bool{"accessToken": current.AccessToken != nil && *current.AccessToken != ""},- map[string]bool{"accessToken": accessTokenUpdated},+ map[string]bool{"accessToken": kit.FromPtr(current.AccessToken) != ""},+ map[string]bool{"accessToken": func() bool {+ _, accessTokenUpdated := updates["access_token"]+ return accessTokenUpdated+ }()},); validateCredentialTargetChangeErr != nil {return nil, validateCredentialTargetChangeErr}@@ -469,9 +505,12 @@ func (s *EnvironmentService) DeleteEnvironment(ctx context.Context, id string, u}}- s.edgeTokens.invalidate(id)+ s.edgeTokens.sync(id, "")s.ForgetSyncState(id)- s.remoteEnvs.remove(id)+ s.remoteEnvs.mu.Lock()+ delete(s.remoteEnvs.envs, id)+ s.remoteEnvs.revision+++ s.remoteEnvs.mu.Unlock()s.invalidateEnvironmentCache(id)s.NotifyRuntimeStateChanged()@@ -489,10 +528,6 @@ func (s *EnvironmentService) createEnvironmentEvent(severity event.EventSeverity,userID, username *string,) {- if s.eventService == nil {- return- }-_, _ = s.eventService.CreateEvent(ctx, event.CreateEventRequest{Type: eventType,Severity: severity,@@ -509,9 +544,6 @@ func (s *EnvironmentService) createEnvironmentEvent(// RegenerateEnvironmentApiKey rotates the key and updates its environment binding.func (s *EnvironmentService) RegenerateEnvironmentApiKey(ctx context.Context, env *Environment, userID, username string) (string, error) {- if env == nil {- return "", errors.New("environment is required")- }return s.swarm.EnsureApiKey(ctx, env.ID, env.AccessToken, env.ApiKeyID, true,func(linkCtx context.Context, apiKeyID, apiKey string) error {return s.linkEnvironmentApiKey(linkCtx, env.ID, apiKeyID, apiKey, userID, username, env.Name)@@ -522,14 +554,12 @@ func (s *EnvironmentService) linkEnvironmentApiKey(ctx context.Context, envID, n// Persisted, cached, and returned keys must stay byte-identical: token lookup is direct equality.apiKey = strings.TrimSpace(apiKey)- updates := map[string]any{+ result := s.db.WithContext(ctx).Model(&Environment{}).Where("id = ?", envID).Updates(map[string]any{"api_key_id": newApiKeyID,"access_token": apiKey,"status": string(EnvironmentStatusPending),"last_seen": nil,- }-- result := s.db.WithContext(ctx).Model(&Environment{}).Where("id = ?", envID).Updates(updates)+ })if result.Error != nil {return fmt.Errorf("failed to update environment with new API key: %w", result.Error)}@@ -541,14 +571,30 @@ func (s *EnvironmentService) linkEnvironmentApiKey(ctx context.Context, envID, ns.NotifyRuntimeStateChanged()s.edgeTokens.sync(envID, apiKey)- now := time.Now()- s.remoteEnvs.update(envID, func(env *Environment) {- env.ApiKeyID = &newApiKeyID- env.AccessToken = &apiKey- env.Status = string(EnvironmentStatusPending)- env.LastSeen = nil- env.UpdatedAt = &now- })+ s.remoteEnvs.mu.Lock()+ s.remoteEnvs.revision+++ if envRecord, ok := s.remoteEnvs.envs[envID]; ok {+ s.remoteEnvs.envs[envID] = Environment{ //exhaustruct:enforce+ BaseModel: database.BaseModel{+ ID: envRecord.ID,+ CreatedAt: envRecord.CreatedAt,+ UpdatedAt: new(time.Now()),+ },+ Name: envRecord.Name,+ ApiUrl: envRecord.ApiUrl,+ Status: string(EnvironmentStatusPending),+ Enabled: envRecord.Enabled,+ IsEdge: envRecord.IsEdge,+ Hidden: envRecord.Hidden,+ LastSeen: nil,+ LastEdgeTransport: envRecord.LastEdgeTransport,+ AccessToken: &apiKey,+ ApiKeyID: &newApiKeyID,+ ParentEnvironmentID: envRecord.ParentEnvironmentID,+ SwarmNodeID: envRecord.SwarmNodeID,+ }+ }+ s.remoteEnvs.mu.Unlock()go s.createEnvironmentEvent(context.WithoutCancel(ctx), envID, envName, event.EventTypeEnvironmentApiKeyRegenerated,"API Key Regenerated", "Environment API key was regenerated and status set to pending", event.EventSeverityInfo, new(userID), new(username))@@ -640,11 +686,6 @@ func (s *EnvironmentService) SyncResourcesToEnvironment(ctx context.Context, env})}-type resourceSync struct {- kind string- sync func(ctx context.Context, environmentID string) error-}-// resourceSyncs lists the manager-owned resource groups pushed to remote environments.func (s *EnvironmentService) resourceSyncs() []resourceSync {return []resourceSync{@@ -671,10 +712,13 @@ const (// GetActiveRemoteEnvironmentSnapshot returns the latest in-process snapshot for// an enabled, visible, non-local remote environment.func (s *EnvironmentService) GetActiveRemoteEnvironmentSnapshot(environmentID string) mo.Option[Environment] {- if s == nil {+ s.remoteEnvs.mu.RLock()+ envRecord, ok := s.remoteEnvs.envs[environmentID]+ s.remoteEnvs.mu.RUnlock()+ if !ok || !isActiveRemoteEnvironment(envRecord) {return mo.None[Environment]()}- return s.remoteEnvs.get(environmentID)+ return mo.Some(envRecord)}// ListActiveRemoteEnvironments returns every enabled, visible, non-local@@ -728,16 +772,111 @@ func (s *EnvironmentService) GetEnvironmentByIDCached(ctx context.Context, id st// invalidateEnvironmentCache starts a new generation for id's stripe after a// committed write, orphaning its cached record and in-flight loads.func (s *EnvironmentService) invalidateEnvironmentCache(id string) {- if s == nil || s.environmentCache == nil {- return- }previous := s.environmentGens[maphash.String(environmentCacheSeed, id)%environmentCacheStripes].Add(1) - 1s.environmentCache.Delete(environmentCacheKey{gen: previous, id: id})}func (s *EnvironmentService) ListEnvironmentsPaginated(ctx context.Context, params pagination.QueryParams, accessibleEnvIDs []string) ([]environment.Environment, pagination.Response, error) {if strings.TrimSpace(params.Filters["type"]) != "" {- return s.listEnvironmentsPaginatedWithRuntimeFilters(ctx, params, accessibleEnvIDs)+ var envs []Environment+ if err := s.db.WithContext(ctx).+ Model(&Environment{}).+ Where("hidden = ?", false).+ Find(&envs).Error; err != nil {+ return nil, pagination.Response{}, fmt.Errorf("failed to list environments: %w", err)+ }++ items, mapErr := mapping.MapSlice[Environment, environment.Environment](envs)+ if mapErr != nil {+ return nil, pagination.Response{}, fmt.Errorf("failed to map environments: %w", mapErr)+ }++ // nil means no restriction; non-nil restricts to the caller's accessible environments.+ if accessibleEnvIDs != nil {+ items = slices.DeleteFunc(items, func(item environment.Environment) bool { return !slices.Contains(accessibleEnvIDs, item.ID) })+ }++ rows := make([]environment.Environment, 0, len(items))+ for _, item := range items {+ rows = append(rows, ApplyEnvironmentRuntimeState(item))+ }++ result := pagination.Config[environment.Environment]{+ SearchAccessors: []pagination.SearchAccessor[environment.Environment]{+ func(env environment.Environment) (string, error) { return env.Name, nil },+ func(env environment.Environment) (string, error) { return env.ApiUrl, nil },+ },+ SortBindings: []pagination.SortBinding[environment.Environment]{+ {+ Key: "id",+ Fn: func(a, b environment.Environment) int {+ return strings.Compare(strings.ToLower(a.ID), strings.ToLower(b.ID))+ },+ },+ {+ Key: "name",+ Fn: func(a, b environment.Environment) int {+ return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))+ },+ },+ {+ Key: "status",+ Fn: func(a, b environment.Environment) int {+ return strings.Compare(strings.ToLower(a.Status), strings.ToLower(b.Status))+ },+ },+ {+ Key: "enabled",+ Fn: func(a, b environment.Environment) int {+ if a.Enabled == b.Enabled {+ return 0+ }+ return kit.Ternary(a.Enabled, 1, -1)+ },+ },+ {+ Key: "apiUrl",+ Fn: func(a, b environment.Environment) int {+ return strings.Compare(strings.ToLower(a.ApiUrl), strings.ToLower(b.ApiUrl))+ },+ },+ },+ FilterAccessors: []pagination.FilterAccessor[environment.Environment]{+ {+ Key: "status",+ Fn: func(item environment.Environment, filterValue string) bool {+ return strings.EqualFold(item.Status, strings.TrimSpace(filterValue))+ },+ },+ {+ Key: "enabled",+ Fn: func(item environment.Environment, filterValue string) bool {+ value, valid := kit.ParseBool(filterValue)+ return !valid || item.Enabled == value+ },+ },+ {+ Key: "type",+ Fn: func(item environment.Environment, filterValue string) bool {+ if !item.IsEdge {+ return strings.ToLower(strings.TrimSpace(filterValue)) == "http"+ }+ // Disconnected or poll-only agents classify by the transport they last used.+ switch strings.ToLower(strings.TrimSpace(kit.Ternary(kit.FromPtr(item.Connected) && item.EdgeTransport != nil,+ kit.FromPtr(item.EdgeTransport), kit.FromPtr(item.LastEdgeTransport)))) {+ case edge.EdgeTransportWebSocket:+ return strings.ToLower(strings.TrimSpace(filterValue)) == "websocket"+ case edge.EdgeTransportGRPC:+ return strings.ToLower(strings.TrimSpace(filterValue)) == "grpc"+ default:+ return strings.ToLower(strings.TrimSpace(filterValue)) == "edge"+ }+ },+ },+ },+ }.SearchOrderAndPaginate(rows, params)++ return result.Items, pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params), nil}var envs []Environment@@ -776,120 +915,6 @@ func (s *EnvironmentService) ListEnvironmentsPaginated(ctx context.Context, parareturn out, paginationResp, nil}-func (s *EnvironmentService) listEnvironmentsPaginatedWithRuntimeFilters(- ctx context.Context,- params pagination.QueryParams,- accessibleEnvIDs []string,-) ([]environment.Environment, pagination.Response, error) {- var envs []Environment- if err := s.db.WithContext(ctx).- Model(&Environment{}).- Where("hidden = ?", false).- Find(&envs).Error; err != nil {- return nil, pagination.Response{}, fmt.Errorf("failed to list environments: %w", err)- }-- items, mapErr := mapping.MapSlice[Environment, environment.Environment](envs)- if mapErr != nil {- return nil, pagination.Response{}, fmt.Errorf("failed to map environments: %w", mapErr)- }-- // nil means no restriction; non-nil restricts to the caller's accessible environments.- if accessibleEnvIDs != nil {- items = slices.DeleteFunc(items, func(item environment.Environment) bool { return !slices.Contains(accessibleEnvIDs, item.ID) })- }-- for i := range items {- ApplyEnvironmentRuntimeState(&items[i])- }-- config := pagination.Config[environment.Environment]{- SearchAccessors: []pagination.SearchAccessor[environment.Environment]{- func(env environment.Environment) (string, error) { return env.Name, nil },- func(env environment.Environment) (string, error) { return env.ApiUrl, nil },- },- SortBindings: []pagination.SortBinding[environment.Environment]{- {- Key: "id",- Fn: func(a, b environment.Environment) int {- return strings.Compare(strings.ToLower(a.ID), strings.ToLower(b.ID))- },- },- {- Key: "name",- Fn: func(a, b environment.Environment) int {- return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))- },- },- {- Key: "status",- Fn: func(a, b environment.Environment) int {- return strings.Compare(strings.ToLower(a.Status), strings.ToLower(b.Status))- },- },- {- Key: "enabled",- Fn: func(a, b environment.Environment) int {- if a.Enabled == b.Enabled {- return 0- }- return kit.Ternary(a.Enabled, 1, -1)- },- },- {- Key: "apiUrl",- Fn: func(a, b environment.Environment) int {- return strings.Compare(strings.ToLower(a.ApiUrl), strings.ToLower(b.ApiUrl))- },- },- },- FilterAccessors: []pagination.FilterAccessor[environment.Environment]{- {- Key: "status",- Fn: func(item environment.Environment, filterValue string) bool {- return strings.EqualFold(item.Status, strings.TrimSpace(filterValue))- },- },- {- Key: "enabled",- Fn: func(item environment.Environment, filterValue string) bool {- value, valid := kit.ParseBool(filterValue)- return !valid || item.Enabled == value- },- },- {- Key: "type",- Fn: func(item environment.Environment, filterValue string) bool {- typeKey := "http"- if item.IsEdge {- // Disconnected or poll-only agents classify by the transport they last used.- transport := ""- if item.Connected != nil && *item.Connected && item.EdgeTransport != nil {- transport = *item.EdgeTransport- } else if item.LastEdgeTransport != nil {- transport = *item.LastEdgeTransport- }- switch strings.ToLower(strings.TrimSpace(transport)) {- case edge.EdgeTransportWebSocket:- typeKey = "websocket"- case edge.EdgeTransportGRPC:- typeKey = "grpc"- default:- typeKey = "edge"- }- }- return typeKey == strings.ToLower(strings.TrimSpace(filterValue))- },- },- },- }-- result := config.SearchOrderAndPaginate(items, params)- paginationResp := pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params)-- return result.Items, paginationResp, nil-}-// ListVisibleEnvironments returns persisted rows; callers apply ApplyEnvironmentRuntimeState.func (s *EnvironmentService) ListVisibleEnvironments(ctx context.Context) ([]environment.Environment, error) {var envs []Environment@@ -961,15 +986,35 @@ func (s *EnvironmentService) ListRemoteEnvironments(ctx context.Context) ([]Envi// SubscribeRuntimeChanges returns a channel that receives a coalesced wake-up// whenever environment liveness may have changed, plus a function to release it.func (s *EnvironmentService) SubscribeRuntimeChanges() (<-chan struct{}, func()) {- return s.runtimeWatchers.subscribe()+ w := &s.runtimeWatchers+ w.mu.Lock()+ defer w.mu.Unlock()++ w.seq+++ id := w.seq+ // Capacity 1: watchers read live state, so a second pending signal is redundant.+ ch := make(chan struct{}, 1)+ w.chans[id] = ch++ return ch, func() {+ w.mu.Lock()+ defer w.mu.Unlock()+ delete(w.chans, id)+ }}-// NotifyRuntimeStateChanged wakes every runtime watcher.+// NotifyRuntimeStateChanged wakes every runtime watcher without blocking, so+// tunnel callbacks may call it.func (s *EnvironmentService) NotifyRuntimeStateChanged() {- if s == nil {- return+ s.runtimeWatchers.mu.Lock()+ defer s.runtimeWatchers.mu.Unlock()++ for _, ch := range s.runtimeWatchers.chans {+ select {+ case ch <- struct{}{}:+ default:+ }}- s.runtimeWatchers.notify()}const (@@ -1038,15 +1083,14 @@ func (s *EnvironmentService) RunHealthChecksNow(ctx context.Context) error {return err}parent, hasParent := jobcontext.Run(ctx)- request := scheduler.Request{EnvironmentID: LocalEnvironmentID, Trigger: "internal"}- if hasParent {- request.Trigger = parent.Trigger- request.RequestedBy = parent.RequestedBy- request.RequestedWithKey = parent.RequestedWithKey- }for _, id := range ids {- request.JobID = s.jobs.JobName(id)- if _, submitErr := s.jobs.Scheduler().Submit(ctx, request); submitErr != nil {+ if _, submitErr := s.jobs.Scheduler().Submit(ctx, scheduler.Request{+ JobID: s.jobs.JobName(id),+ EnvironmentID: LocalEnvironmentID,+ Trigger: kit.Ternary(hasParent, parent.Trigger, "internal"),+ RequestedBy: parent.RequestedBy,+ RequestedWithKey: parent.RequestedWithKey,+ }); submitErr != nil {return submitErr}}@@ -1148,11 +1192,7 @@ func (s *EnvironmentService) TestConnection(ctx context.Context, id string, custreturn "error", err}- apiURL := envRecord.ApiUrl- if customApiUrl != nil {- apiURL = *customApiUrl- }- status, err := s.health.Probe(ctx, id, apiURL, envRecord.IsEdge, customApiUrl != nil)+ status, err := s.health.Probe(ctx, id, mo.PointerToOption(customApiUrl).OrElse(envRecord.ApiUrl), envRecord.IsEdge, customApiUrl != nil)if customApiUrl == nil {if updateErr := s.updateEnvironmentStatus(ctx, id, status); updateErr != nil {slog.WarnContext(ctx, "Failed to persist environment status", "environmentId", id, "status", status, "error", updateErr)@@ -1248,65 +1288,48 @@ func (s *EnvironmentService) UpdateEnvironmentConnectionState(ctx context.Contex// ApplyEnvironmentRuntimeState overlays in-memory tunnel and poll state onto an// edge environment without mutating persisted state.-func ApplyEnvironmentRuntimeState(env *environment.Environment) {- if env == nil || !env.IsEdge {- return- }-- connected := false- env.Connected = &connected- env.ConnectedAt = nil- env.LastHeartbeat = nil- env.LastPollAt = nil- env.EdgeTransport = nil- env.EdgeSecurityMode = nil- env.EdgeSessionID = nil- env.EdgeAgentInstance = nil- env.EdgeCapabilities = nil- env.EdgeDataPlane = nil- env.EdgeRoundTripMs = nil- env.EdgeOpenStreams = nil-- if pollState, ok := edge.GetPollRuntimeRegistry().Get(env.ID, time.Now()).Get(); ok {- env.LastPollAt = pollState.LastPollAt- }-- if runtimeState, ok := edge.GetTunnelRuntimeState(env.ID).Get(); ok {- connected = true- env.Connected = &connected- env.Status = string(EnvironmentStatusOnline)- env.ConnectedAt = runtimeState.ConnectedAt- env.LastHeartbeat = runtimeState.LastHeartbeat- if runtimeState.SecurityMode != "" {- env.EdgeSecurityMode = &runtimeState.SecurityMode- }- if runtimeState.SessionID != "" {- env.EdgeSessionID = &runtimeState.SessionID- }- if runtimeState.AgentInstance != "" {- env.EdgeAgentInstance = &runtimeState.AgentInstance- }- if len(runtimeState.Capabilities) > 0 {- env.EdgeCapabilities = append([]string(nil), runtimeState.Capabilities...)- }- if runtimeState.Transport != "" {- env.EdgeTransport = &runtimeState.Transport- }- env.EdgeDataPlane = &runtimeState.DataPlane- env.EdgeOpenStreams = &runtimeState.OpenStreams- if runtimeState.RoundTripTime != nil {- env.EdgeRoundTripMs = new(float64(runtimeState.RoundTripTime.Microseconds()) / 1000)- }- return- }-- if env.LastPollAt != nil {- env.Status = string(EnvironmentStatusStandby)- return- }-- if env.Status != string(EnvironmentStatusPending) {- env.Status = string(EnvironmentStatusOffline)+func ApplyEnvironmentRuntimeState(env environment.Environment) environment.Environment {+ if !env.IsEdge {+ return env+ }++ pollState, _ := edge.GetPollRuntimeRegistry().Get(env.ID, time.Now()).Get()+ runtimeState, connected := edge.GetTunnelRuntimeState(env.ID).Get()+ return environment.Environment{ //exhaustruct:enforce+ ID: env.ID,+ Name: env.Name,+ ApiUrl: env.ApiUrl,+ Status: func() string {+ switch {+ case connected:+ return string(EnvironmentStatusOnline)+ case pollState.LastPollAt != nil:+ return string(EnvironmentStatusStandby)+ case env.Status != string(EnvironmentStatusPending):+ return string(EnvironmentStatusOffline)+ default:+ return env.Status+ }+ }(),+ Enabled: env.Enabled,+ IsEdge: env.IsEdge,+ LastSeen: env.LastSeen,+ EdgeTransport: kit.Ternary(kit.FromPtr(runtimeState).Transport != "", new(kit.FromPtr(runtimeState).Transport), nil),+ LastEdgeTransport: env.LastEdgeTransport,+ EdgeSecurityMode: kit.Ternary(kit.FromPtr(runtimeState).SecurityMode != "", new(kit.FromPtr(runtimeState).SecurityMode), nil),+ EdgeSessionID: kit.Ternary(kit.FromPtr(runtimeState).SessionID != "", new(kit.FromPtr(runtimeState).SessionID), nil),+ EdgeAgentInstance: kit.Ternary(kit.FromPtr(runtimeState).AgentInstance != "", new(kit.FromPtr(runtimeState).AgentInstance), nil),+ EdgeCapabilities: kit.Ternary(len(kit.FromPtr(runtimeState).Capabilities) > 0, slices.Clone(kit.FromPtr(runtimeState).Capabilities), nil),+ EdgeDataPlane: kit.Ternary(connected, new(kit.FromPtr(runtimeState).DataPlane), nil),+ EdgeRoundTripMs: kit.Ternary(kit.FromPtr(runtimeState).RoundTripTime != nil,+ new(float64(kit.FromPtr(kit.FromPtr(runtimeState).RoundTripTime).Microseconds())/1000), nil),+ EdgeOpenStreams: kit.Ternary(connected, new(kit.FromPtr(runtimeState).OpenStreams), nil),+ Connected: new(connected),+ ConnectedAt: kit.FromPtr(runtimeState).ConnectedAt,+ LastHeartbeat: kit.FromPtr(runtimeState).LastHeartbeat,+ LastPollAt: pollState.LastPollAt,+ EdgeMTLSCertificate: env.EdgeMTLSCertificate,+ ApiKey: env.ApiKey,}}@@ -1350,7 +1373,7 @@ func (s *EnvironmentService) syncToRemoteEnvironments(ctx context.Context, kindvar failedCount intfor _, env := range envs {- if env.AccessToken == nil || strings.TrimSpace(*env.AccessToken) == "" {+ if strings.TrimSpace(kit.FromPtr(env.AccessToken)) == "" {slog.DebugContext(ctx, "Skipping sync for environment without access token", "kind", kind, "environmentId", env.ID, "environmentName", env.Name)continue}@@ -1375,12 +1398,11 @@ func (s *EnvironmentService) CheckS3DestinationReferences(ctx context.Context, d}for _, env := range envs {// Environments without an access token never receive destination syncs.- if env.AccessToken == nil || strings.TrimSpace(*env.AccessToken) == "" {+ if strings.TrimSpace(kit.FromPtr(env.AccessToken)) == "" {continue}var result backup.S3DestinationUsage- path := "/api/backups/s3/" + url.PathEscape(destinationID) + "/in-use"- if proxyErr := s.ProxyJSONRequestForEnvironment(ctx, env, http.MethodGet, path, nil, &result); proxyErr != nil {+ if proxyErr := s.ProxyJSONRequestForEnvironment(ctx, env, http.MethodGet, "/api/backups/s3/"+url.PathEscape(destinationID)+"/in-use", nil, &result); proxyErr != nil {return fmt.Errorf("cannot verify S3 destination references on environment %s; restore connectivity before deleting: %w", env.Name, proxyErr)}if result.InUse {@@ -1446,29 +1468,6 @@ func (s *EnvironmentService) ProxyRequest(ctx context.Context, envID, method, pareturn resp.Body, resp.StatusCode, nil}-// DeploymentSnippets contains deployment configuration snippets for an environment.-type DeploymentSnippets struct {- DockerRun string- DockerCompose string- MTLS *DeploymentSnippetMTLS-}--type DeploymentSnippetFile struct {- Name string `json:"name" doc:"Suggested filename"`- Content string `json:"content,omitempty" doc:"PEM file contents. Omitted for sensitive files such as private keys; use downloadUrl instead."`- DownloadURL string `json:"downloadUrl,omitempty" doc:"Pairing-permission endpoint to download this file when content is withheld"`- Sensitive bool `json:"sensitive,omitempty" doc:"True when this file is sensitive and must be fetched via downloadUrl"`- ContainerPath string `json:"containerPath" doc:"Container mount path expected by the mTLS snippet"`- Permissions string `json:"permissions" doc:"Suggested file mode"`-}--type DeploymentSnippetMTLS struct {- DockerRun string `json:"dockerRun" doc:"Docker run snippet using Arcane-generated mTLS assets"`- DockerCompose string `json:"dockerCompose" doc:"Docker compose snippet using Arcane-generated mTLS assets"`- Files []DeploymentSnippetFile `json:"files" doc:"Generated PEM files to place on the edge host"`- HostDirHint string `json:"hostDirHint" doc:"Suggested host directory containing the generated PEM files"`-}-// GenerateDeploymentSnippets generates Docker deployment snippets for an environment.func (s *EnvironmentService) GenerateDeploymentSnippets(ctx context.Context, envID, managerURL, agentURL, apiKey string) (*DeploymentSnippets, error) {dockerRun, dockerCompose := snippets.Direct(strings.TrimRight(managerURL, "/"), agentURL, apiKey)@@ -1483,24 +1482,45 @@ func (s *EnvironmentService) GenerateDeploymentSnippets(ctx context.Context, envfunc (s *EnvironmentService) GenerateEdgeDeploymentSnippets(ctx context.Context, envID, managerURL, apiKey string, edgeCfg *edge.Config) (*DeploymentSnippets, error) {managerURL = strings.TrimRight(managerURL, "/")dockerRun, dockerCompose := snippets.Edge(managerURL, apiKey)- result := &DeploymentSnippets{- DockerRun: dockerRun,- DockerCompose: dockerCompose,- }-- envName := ""- if s != nil && s.db != nil {- if env, getErr := s.GetEnvironmentByID(ctx, envID); getErr == nil && env != nil {- envName = env.Name- }- }- if edgeCfg != nil && strings.TrimSpace(edgeCfg.AppURL) == "" {- edgeCfg.AppURL = managerURL- }-- generatedAssets, mtlsDockerRun, mtlsDockerCompose := s.snippets.GenerateMTLS(ctx, edgeCfg, envID, envName, managerURL, apiKey)+ generatedAssets, mtlsDockerRun, mtlsDockerCompose := s.snippets.GenerateMTLS(ctx,+ func() *edge.Config {+ if edgeCfg == nil {+ return nil+ }+ return &edge.Config{ //exhaustruct:enforce+ EdgeAgent: edgeCfg.EdgeAgent,+ EdgeTransport: edgeCfg.EdgeTransport,+ EdgeReconnectInterval: edgeCfg.EdgeReconnectInterval,+ EdgeMTLSMode: edgeCfg.EdgeMTLSMode,+ EdgeMTLSCAFile: edgeCfg.EdgeMTLSCAFile,+ EdgeMTLSCertFile: edgeCfg.EdgeMTLSCertFile,+ EdgeMTLSKeyFile: edgeCfg.EdgeMTLSKeyFile,+ EdgeMTLSServerName: edgeCfg.EdgeMTLSServerName,+ EdgeMTLSAssetsDir: edgeCfg.EdgeMTLSAssetsDir,+ AppURL: kit.Ternary(strings.TrimSpace(edgeCfg.AppURL) == "", managerURL, edgeCfg.AppURL),+ ManagerApiUrl: edgeCfg.ManagerApiUrl,+ AgentToken: edgeCfg.AgentToken,+ Port: edgeCfg.Port,+ Listen: edgeCfg.Listen,+ EdgeMuxDisabled: edgeCfg.EdgeMuxDisabled,+ }+ }(),+ envID,+ func() string {+ env, getErr := s.GetEnvironmentByID(ctx, envID)+ if getErr != nil {+ return ""+ }+ return env.Name+ }(),+ managerURL,+ apiKey,+ )if generatedAssets == nil {- return result, nil+ return &DeploymentSnippets{+ DockerRun: dockerRun,+ DockerCompose: dockerCompose,+ }, nil}files := make([]DeploymentSnippetFile, 0, len(generatedAssets.Files))@@ -1512,13 +1532,16 @@ func (s *EnvironmentService) GenerateEdgeDeploymentSnippets(ctx context.Context,Permissions: file.Permissions,})}- result.MTLS = &DeploymentSnippetMTLS{- DockerRun: mtlsDockerRun,- DockerCompose: mtlsDockerCompose,- Files: files,- HostDirHint: strings.TrimSpace(generatedAssets.HostDirHint),- }- return result, nil+ return &DeploymentSnippets{+ DockerRun: dockerRun,+ DockerCompose: dockerCompose,+ MTLS: &DeploymentSnippetMTLS{+ DockerRun: mtlsDockerRun,+ DockerCompose: mtlsDockerCompose,+ Files: files,+ HostDirHint: strings.TrimSpace(generatedAssets.HostDirHint),+ },+ }, nil}func (s *EnvironmentService) ListSwarmNodeAgentEnvironments(ctx context.Context, parentEnvironmentID string) ([]Environment, error) {diff --git a/backend/internal/environment/service_test.go b/backend/internal/environment/service_test.goindex 26d4ed04b..2fc853b14 100644--- a/backend/internal/environment/service_test.go+++ b/backend/internal/environment/service_test.go@@ -24,6 +24,7 @@ import ("github.com/libtnb/sqlite""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ kit "go.getarcane.app/kit/pkg""go.getarcane.app/sys/crypto""gorm.io/gorm"@@ -32,6 +33,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/config""github.com/getarcaneapp/arcane/backend/v2/internal/database""github.com/getarcaneapp/arcane/backend/v2/internal/environment/children/snippets"+ "github.com/getarcaneapp/arcane/backend/v2/internal/event""github.com/getarcaneapp/arcane/backend/v2/internal/gitrepo""github.com/getarcaneapp/arcane/backend/v2/internal/registry""github.com/getarcaneapp/arcane/backend/v2/internal/s3"@@ -45,6 +47,31 @@ import (francistest "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/francis/testing")+type environmentTestScheduler struct {+ submitted []scheduler.Request+ added []string+ removed []string+}++// Project imports environment, so these rows avoid an import cycle in tests.+type testProjectRow struct {+ database.BaseModel+ Name string+ Path string+ Status string+ GitOpsManagedBy *string `gorm:"column:gitops_managed_by"`+}++type testGitOpsSyncRow struct {+ database.BaseModel+ Name string+ EnvironmentID string+ RepositoryID string+ ComposePath string+ ProjectName string+ SyncInterval int+}+func TestEnvironmentService_OverlappingHealthCheckIsSkipped(t *testing.T) {gate := newAdmissionGateForEnvironmentTest(t)@@ -53,7 +80,7 @@ func TestEnvironmentService_OverlappingHealthCheckIsSkipped(t *testing.T) {require.NoError(t, err)require.True(t, admitted)- service := &EnvironmentService{jobs: entityjobs.New(environmentHealthJobPrefix, environmentHealthAdmissionScope)}+ service := NewEnvironmentService(nil, http.DefaultClient, nil, nil, nil, nil)require.NoError(t, service.SetScheduler(t.Context(), &environmentTestScheduler{}, gate))outcome, runErr := service.runHealthCheck(t.Context(), "environment-id")require.NoError(t, runErr)@@ -61,12 +88,6 @@ func TestEnvironmentService_OverlappingHealthCheckIsSkipped(t *testing.T) {lease.Release(t.Context())}-type environmentTestScheduler struct {- submitted []scheduler.Request- added []string- removed []string-}-func (s *environmentTestScheduler) AddJob(_ context.Context, job scheduler.Job) error {s.added = append(s.added, job.Name())return nil@@ -89,27 +110,8 @@ func newAdmissionGateForEnvironmentTest(t testing.TB) *runs.Admission {return gate}-// Project imports environment, so these rows avoid an import cycle in tests.-type testProjectRow struct {- database.BaseModel- Name string- Path string- Status string- GitOpsManagedBy *string `gorm:"column:gitops_managed_by"`-}-func (testProjectRow) TableName() string { return "projects" }-type testGitOpsSyncRow struct {- database.BaseModel- Name string- EnvironmentID string- RepositoryID string- ComposePath string- ProjectName string- SyncInterval int-}-func (testGitOpsSyncRow) TableName() string { return "gitops_syncs" }func setupEnvironmentServiceTestDB(t *testing.T) *database.DB {@@ -125,6 +127,7 @@ func setupEnvironmentServiceTestDB(t *testing.T) *database.DB {&settings.SettingVariable{},&user.User{},&apikey.ApiKey{},+ &event.Event{},&testProjectRow{},&testGitOpsSyncRow{},))@@ -227,7 +230,7 @@ func TestEnvironmentService_DeleteEnvironment_CascadesGitOpsSyncs(t *testing.T)}).Error)jobScheduler := &environmentTestScheduler{}- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)require.NoError(t, svc.SetScheduler(ctx, jobScheduler, newAdmissionGateForEnvironmentTest(t)))require.NoError(t, svc.DeleteEnvironment(ctx, "env-delete-gitops", nil, nil))@@ -309,7 +312,7 @@ func encryptSecretForTest(t *testing.T, value string) string {func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SyncsEligibleRemotes(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestRegistry(t, db, "reg-1")@@ -386,7 +389,7 @@ func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SyncsEligibleRemofunc TestEnvironmentService_SyncRegistriesToEnvironment_IncludesECRFields(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestECRRegistry(t, db, "reg-ecr")@@ -443,7 +446,7 @@ func TestEnvironmentService_SyncRegistriesToEnvironment_IncludesECRFields(t *tesfunc TestEnvironmentService_SyncS3DestinationsToEnvironment(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)encryptedSecret, err := crypto.Encrypt("s3-secret")require.NoError(t, err)@@ -505,7 +508,7 @@ func TestEnvironmentService_SyncRepositoriesToEnvironment_UsesAgentHeaders(t *tectx := t.Context()db := setupEnvironmentServiceTestDB(t)require.NoError(t, db.AutoMigrate(&gitrepo.GitRepository{}))- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestGitRepository(t, db, gitrepo.GitRepository{ID: "repo-1", CreatedAt: time.Now(),@@ -561,7 +564,7 @@ func TestEnvironmentService_SyncRepositoriesToEnvironment_UsesAgentHeaders(t *tefunc TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SkipsRemoteWithoutAccessToken(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestRegistry(t, db, "reg-1")@@ -584,7 +587,7 @@ func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_SkipsRemoteWithoufunc TestEnvironmentService_SyncRegistriesToRemoteEnvironments_ReportsFailuresButContinues(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestRegistry(t, db, "reg-1")@@ -608,7 +611,7 @@ func TestEnvironmentService_SyncRegistriesToRemoteEnvironments_ReportsFailuresBufunc TestEnvironmentService_ReconcileEdgeStatusesOnStartup(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironmentWithState(t, db, "edge-online", "edge://online", string(EnvironmentStatusOnline), true, nil)createTestEnvironmentWithState(t, db, "edge-error", "edge://error", string(EnvironmentStatusError), true, nil)@@ -638,7 +641,7 @@ func TestEnvironmentService_ReconcileEdgeStatusesOnStartup(t *testing.T) {func TestEnvironmentService_UpdateEnvironmentConnectionState(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironmentWithState(t, db, "edge-runtime", "edge://runtime", string(EnvironmentStatusOffline), true, nil)@@ -664,7 +667,7 @@ func TestEnvironmentService_UpdateEnvironmentConnectionState(t *testing.T) {func TestEnvironmentService_UpdateEnvironmentStatus_PromotesPendingDirectEnv(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironmentWithState(t, db, "direct-pending", "http://agent:3553", string(EnvironmentStatusPending), false, nil)@@ -679,7 +682,7 @@ func TestEnvironmentService_UpdateEnvironmentStatus_PromotesPendingDirectEnv(t *func TestEnvironmentService_UpdateEnvironmentStatus_DoesNotDemotePendingDirectEnvOnFailedTick(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironmentWithState(t, db, "direct-pending", "http://agent:3553", string(EnvironmentStatusPending), false, nil)@@ -696,7 +699,7 @@ func TestEnvironmentService_UpdateEnvironmentStatus_DoesNotDemotePendingDirectEnfunc TestEnvironmentService_UpdateEnvironmentStatus_LeavesPendingEdgeEnvAlone(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironmentWithState(t, db, "edge-pending", "edge://agent", string(EnvironmentStatusPending), true, nil)@@ -713,7 +716,7 @@ func TestEnvironmentService_UpdateEnvironmentStatus_LeavesPendingEdgeEnvAlone(tfunc TestEnvironmentService_ResolveEdgeEnvironmentByToken_CachesAndInvalidatesOnUpdate(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)oldToken := "edge-token-old"newToken := "edge-token-new"@@ -743,7 +746,7 @@ func TestEnvironmentService_ResolveEdgeEnvironmentByToken_CachesAndInvalidatesOnfunc TestEnvironmentService_UpdateEnvironment_ClearingAccessTokenInvalidatesCache(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)oldToken := "edge-token-clear"createTestEnvironmentWithState(t, db, "edge-auth-clear", "edge://auth-clear", string(EnvironmentStatusPending), true, &oldToken)@@ -755,10 +758,6 @@ func TestEnvironmentService_UpdateEnvironment_ClearingAccessTokenInvalidatesCach_, err = svc.UpdateEnvironment(ctx, "edge-auth-clear", map[string]any{"access_token": nil}, nil, nil)require.NoError(t, err)- cachedEnvID, ok := svc.edgeTokens.environmentID(oldToken).Get()- require.False(t, ok)- require.Empty(t, cachedEnvID)-_, tokenStillCached := svc.edgeTokens.byToken.Peek(oldToken)svc.edgeTokens.mu.RLock()_, reverseIndexStillCached := svc.edgeTokens.byEnvID["edge-auth-clear"]@@ -775,7 +774,7 @@ func TestEnvironmentService_UpdateEnvironment_ClearingAccessTokenInvalidatesCachfunc TestEnvironmentServiceUpdateEnvironmentRejectsTargetChangeWithStoredToken(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)oldToken := "stored-agent-token"createTestEnvironment(t, db, "env-target", "http://agent.example:3553", &oldToken)@@ -802,7 +801,7 @@ func TestEnvironmentServiceUpdateEnvironmentAllowsTargetChangeWithExplicitToken(t.Run(tc.name, func(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)oldToken := "stored-agent-token"createTestEnvironment(t, db, "env-target", "http://agent.example:3553", &oldToken)@@ -824,15 +823,16 @@ func TestEnvironmentServiceUpdateEnvironmentAllowsTargetChangeWithExplicitToken(}func TestEnvironmentService_getCachedEnvironmentIDForToken_ExpiresAndCleansReverseIndex(t *testing.T) {- svc := NewEnvironmentService(nil, nil, nil, nil, nil, nil)+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)svc.edgeTokens.mu.Lock()svc.edgeTokens.byEnvID["env-expired"] = "expired-token"svc.edgeTokens.byToken.SetWithTTL("expired-token", "env-expired", time.Nanosecond)time.Sleep(time.Millisecond)svc.edgeTokens.mu.Unlock()- envID, ok := svc.edgeTokens.environmentID("expired-token").Get()- require.False(t, ok)+ envID, err := svc.ResolveEdgeEnvironmentByToken(t.Context(), "expired-token")+ require.ErrorContains(t, err, "invalid agent token")require.Empty(t, envID)_, tokenStillCached := svc.edgeTokens.byToken.Peek("expired-token")@@ -847,7 +847,7 @@ func TestEnvironmentService_getCachedEnvironmentIDForToken_ExpiresAndCleansReverfunc TestEnvironmentService_ResolveEnvironmentByAccessToken(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)accessToken := "remote-token"createNamedTestEnvironment(t, db, "env-remote", "Remote Alpha", "http://remote.example", &accessToken)@@ -864,7 +864,8 @@ func TestEnvironmentService_ResolveEnvironmentByAccessToken(t *testing.T) {}func TestEnvironmentService_GenerateDeploymentSnippets_TransportDefaults(t *testing.T) {- svc := NewEnvironmentService(nil, nil, nil, nil, nil, nil)+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)standard, err := svc.GenerateDeploymentSnippets(t.Context(), "env-1", "https://manager.example.com", "https://agent.example.com", "token-123")require.NoError(t, err)@@ -950,7 +951,7 @@ func TestEnvironmentService_EnsureSwarmNodeAgentEnvironment_CreatesVisibleEnvirodb := setupEnvironmentServiceTestDB(t)userService := user.NewUserService(db, nil, session.RevokeAllUserSessionsExceptInDB)apiKeyService := apikey.NewApiKeyService(db, userService, nil)- svc := NewEnvironmentService(db, nil, nil, nil, nil, apiKeyService)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, apiKeyService)localUser := createTestEnvironmentServiceUser(t, ctx, userService, "swarm-admin")createdEnv, createdToken, err := svc.EnsureSwarmNodeAgentEnvironment(@@ -1020,7 +1021,7 @@ func TestEnvironmentService_EnsureSwarmNodeAgentEnvironment_CreatesVisibleEnvirofunc TestEnvironmentService_EnsureSwarmNodeAgentEnvironment_ReusesLegacyHiddenRegistration(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)parentEnvironmentID := "manager-env"nodeID := "legacy-node"token := "legacy-agent-token"@@ -1084,7 +1085,7 @@ func TestEnvironmentService_EnsureSwarmNodeAgentEnvironment_TokenResolvesEndToEndb := setupEnvironmentServiceTestDB(t)userService := user.NewUserService(db, nil, session.RevokeAllUserSessionsExceptInDB)apiKeyService := apikey.NewApiKeyService(db, userService, nil)- svc := NewEnvironmentService(db, nil, nil, nil, nil, apiKeyService)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, apiKeyService)localUser := createTestEnvironmentServiceUser(t, ctx, userService, "swarm-resolve-admin")createdEnv, createdToken, err := svc.EnsureSwarmNodeAgentEnvironment(@@ -1137,7 +1138,7 @@ func TestEnvironmentService_BindSwarmNodeEnvironment_PreservesVisibleEnvironmentdb := setupEnvironmentServiceTestDB(t)token := "existing-agent-token"createTestEnvironment(t, db, "visible-env", "http://visible.example", &token)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)bound, err := svc.BindSwarmNodeEnvironment(ctx, "manager-env", "node-1", "visible-env", false)require.NoError(t, err)@@ -1158,7 +1159,7 @@ func TestEnvironmentService_BindSwarmNodeEnvironment_RequiresExplicitRebind(t *tctx := t.Context()db := setupEnvironmentServiceTestDB(t)createTestEnvironment(t, db, "visible-env", "http://visible.example", nil)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)_, err := svc.BindSwarmNodeEnvironment(ctx, "manager-a", "node-a", "visible-env", false)require.NoError(t, err)@@ -1174,7 +1175,7 @@ func TestEnvironmentService_BindSwarmNodeEnvironment_RequiresExplicitRebind(t *tfunc TestEnvironmentService_ListMethods_ExcludeHiddenEnvironments(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "0", "http://localhost:3552", nil)createNamedTestEnvironment(t, db, "env-visible", "Visible Remote", "http://visible.example", new("visible-token"))@@ -1209,7 +1210,7 @@ func TestEnvironmentService_ListMethods_ExcludeHiddenEnvironments(t *testing.T)func TestEnvironmentService_ListEnvironmentsPaginated_FiltersByAccessibleEnvIDs(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "0", "http://localhost:3552", nil)createNamedTestEnvironment(t, db, "env-a", "Env A", "http://a.example", new("token-a"))@@ -1252,7 +1253,7 @@ func TestEnvironmentService_ListEnvironmentsPaginated_FiltersByAccessibleEnvIDs(func TestEnvironmentService_ListEnvironmentsPaginated_FiltersByRuntimeType(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)now := time.Now()envs := []Environment{@@ -1387,7 +1388,8 @@ func TestEnvironmentService_GenerateEdgeDeploymentSnippets_WithAutoGeneratedMTLSEncryptionKey: "test-encryption-key-for-testing-32bytes-min",Environment: "test",})- svc := NewEnvironmentService(nil, nil, nil, nil, nil, nil)+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)assetsDir := filepath.Join(t.TempDir(), "edge-mtls")deploymentSnippets, err := svc.GenerateEdgeDeploymentSnippets(t.Context(), "env-mtls", "https://manager.example.com", "token-789", &edge.Config{@@ -1419,7 +1421,8 @@ func TestEnvironmentService_GenerateEdgeDeploymentSnippets_WithAutoGeneratedMTLS}func TestEnvironmentService_GenerateEdgeDeploymentSnippets_ReturnsBasicSnippetsWhenMTLSGenerationFails(t *testing.T) {- svc := NewEnvironmentService(nil, nil, nil, nil, nil, nil)+ db := setupEnvironmentServiceTestDB(t)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)assetsPath := filepath.Join(t.TempDir(), "edge-mtls-file")require.NoError(t, os.WriteFile(assetsPath, []byte("not a directory"), 0o600))@@ -1439,7 +1442,7 @@ func TestEnvironmentService_GenerateEdgeDeploymentSnippets_ReturnsBasicSnippetsWfunc TestEnvironmentService_TestConnection_RejectsInvalidCustomURL(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "env-1", "http://example.com", nil)status, err := svc.TestConnection(ctx, "env-1", new("ftp://example.com"))@@ -1456,7 +1459,7 @@ func TestEnvironmentServiceTestConnectionHidesCustomURLFailure(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)+ svc := NewEnvironmentService(db, server.Client(), nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "env-1", "http://stored.example", nil)status, err := svc.TestConnection(ctx, "env-1", new(server.URL))@@ -1473,7 +1476,7 @@ func TestEnvironmentServiceTestConnectionAllowsStoredPrivateURL(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)+ svc := NewEnvironmentService(db, server.Client(), nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "env-private", server.URL, nil)status, err := svc.TestConnection(ctx, "env-private", nil)@@ -1484,7 +1487,7 @@ func TestEnvironmentServiceTestConnectionAllowsStoredPrivateURL(t *testing.T) {func TestEnvironmentService_ExecuteRemoteRequest_RejectsInvalidEnvironmentURL(t *testing.T) {ctx := t.Context()db := setupEnvironmentServiceTestDB(t)- svc := NewEnvironmentService(db, nil, nil, nil, nil, nil)+ svc := NewEnvironmentService(db, http.DefaultClient, nil, event.NewEventService(db, nil, nil), nil, nil)createTestEnvironment(t, db, "env-invalid-url", "http://user:[email protected]", nil)@@ -1495,7 +1498,12 @@ func TestEnvironmentService_ExecuteRemoteRequest_RejectsInvalidEnvironmentURL(tfunc (s *environmentTestScheduler) Submit(_ context.Context, request scheduler.Request) (scheduler.Run, error) {s.submitted = append(s.submitted, request)- return scheduler.Run{ID: request.RunID, JobID: request.JobID, EnvironmentID: request.EnvironmentID, Status: scheduler.Queued}, nil+ return scheduler.Run{+ ID: request.RunID,+ JobID: request.JobID,+ EnvironmentID: request.EnvironmentID,+ Status: scheduler.Queued,+ }, nil}func setupSyncDB(t *testing.T) *database.DB {@@ -1528,29 +1536,38 @@ func TestSyncCredentialDecryptionAbortsSnapshot(t *testing.T) {_, _ = w.Write([]byte(`{"success":true}`))}))defer server.Close()- require.NoError(t, db.Create(&Environment{ID: "remote", ApiUrl: server.URL, AccessToken: new("agent-token")}).Error)- service := NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)+ require.NoError(t, db.Create(&Environment{+ ID: "remote",+ ApiUrl: server.URL,+ AccessToken: new("agent-token"),+ }).Error)+ service := NewEnvironmentService(db, server.Client(), nil, event.NewEventService(db, nil, nil), nil, nil)var syncResource func(context.Context, string) errorswitch kind {case "registry token", "ECR secret":- row := registry.ContainerRegistry{ID: "bad-credential", URL: "registry.example.com", RegistryType: registry.RegistryTypeGeneric, Token: "invalid-ciphertext"}- if kind == "ECR secret" {- row.RegistryType = registry.RegistryTypeECR- row.AWSSecretAccessKey = "invalid-ciphertext"- }- require.NoError(t, db.Create(&row).Error)+ require.NoError(t, db.Create(®istry.ContainerRegistry{+ ID: "bad-credential",+ URL: "registry.example.com",+ Token: "invalid-ciphertext",+ RegistryType: kit.Ternary(kind == "ECR secret", registry.RegistryTypeECR, registry.RegistryTypeGeneric),+ AWSSecretAccessKey: kit.Ternary(kind == "ECR secret", "invalid-ciphertext", ""),+ }).Error)syncResource = service.SyncRegistriesToEnvironmentcase "repository token", "repository SSH key":- row := gitrepo.GitRepository{ID: "bad-credential", Name: "Repository", URL: "https://example.com/repo.git"}- if kind == "repository token" {- row.Token = "invalid-ciphertext"- } else {- row.SSHKey = "invalid-ciphertext"- }- require.NoError(t, db.Create(&row).Error)+ require.NoError(t, db.Create(&gitrepo.GitRepository{+ ID: "bad-credential",+ Name: "Repository",+ URL: "https://example.com/repo.git",+ Token: kit.Ternary(kind == "repository token", "invalid-ciphertext", ""),+ SSHKey: kit.Ternary(kind == "repository token", "", "invalid-ciphertext"),+ }).Error)syncResource = service.SyncRepositoriesToEnvironmentcase "S3 secret":- require.NoError(t, db.Create(&s3.S3Destination{ID: "bad-credential", Name: "Backup", SecretAccessKey: "invalid-ciphertext"}).Error)+ require.NoError(t, db.Create(&s3.S3Destination{+ ID: "bad-credential",+ Name: "Backup",+ SecretAccessKey: "invalid-ciphertext",+ }).Error)syncResource = service.SyncS3DestinationsToEnvironment}err := syncResource(t.Context(), "remote")@@ -1564,7 +1581,12 @@ func TestSyncCredentialDecryptionAbortsSnapshot(t *testing.T) {func TestSyncRepositoriesPreservesEmptyCredentials(t *testing.T) {db := setupSyncDB(t)- require.NoError(t, db.Create(&gitrepo.GitRepository{ID: "public-repo", Name: "Public", URL: "https://example.com/public.git", AuthType: "none"}).Error)+ require.NoError(t, db.Create(&gitrepo.GitRepository{+ ID: "public-repo",+ Name: "Public",+ URL: "https://example.com/public.git",+ AuthType: "none",+ }).Error)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {var payload gitops.RepositorySyncRequestif err := json.NewDecoder(r.Body).Decode(&payload); err != nil {@@ -1578,8 +1600,12 @@ func TestSyncRepositoriesPreservesEmptyCredentials(t *testing.T) {_, _ = w.Write([]byte(`{"success":true}`))}))defer server.Close()- require.NoError(t, db.Create(&Environment{ID: "remote", ApiUrl: server.URL, AccessToken: new("token")}).Error)- service := NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)+ require.NoError(t, db.Create(&Environment{+ ID: "remote",+ ApiUrl: server.URL,+ AccessToken: new("token"),+ }).Error)+ service := NewEnvironmentService(db, server.Client(), nil, event.NewEventService(db, nil, nil), nil, nil)require.NoError(t, service.SyncRepositoriesToEnvironment(t.Context(), "remote"))}@@ -1600,9 +1626,19 @@ func TestSyncSkipsUnchangedPayloadUntilForgotten(t *testing.T) {defer server.Close()token, err := crypto.Encrypt("secret")require.NoError(t, err)- require.NoError(t, db.Create(®istry.ContainerRegistry{ID: "ghcr", URL: "ghcr.io", RegistryType: registry.RegistryTypeGeneric, Username: "user", Token: token}).Error)- require.NoError(t, db.Create(&Environment{ID: "remote", ApiUrl: server.URL, AccessToken: new("agent-token")}).Error)- service := NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)+ require.NoError(t, db.Create(®istry.ContainerRegistry{+ ID: "ghcr",+ URL: "ghcr.io",+ Username: "user",+ Token: token,+ RegistryType: registry.RegistryTypeGeneric,+ }).Error)+ require.NoError(t, db.Create(&Environment{+ ID: "remote",+ ApiUrl: server.URL,+ AccessToken: new("agent-token"),+ }).Error)+ service := NewEnvironmentService(db, server.Client(), nil, event.NewEventService(db, nil, nil), nil, nil)localSync := func() error { return service.SyncRegistriesToEnvironment(t.Context(), "remote") }changeRegistry := func(url string) {require.NoError(t, db.Model(®istry.ContainerRegistry{}).Where("id = ?", "ghcr").Update("url", url).Error)@@ -1634,7 +1670,7 @@ func TestSyncSkipsUnchangedPayloadUntilForgotten(t *testing.T) {// A watcher must see a change signal, and a burst must not block the notifier —// it runs on the edge tunnel's connect/disconnect path.func TestEnvironmentServiceRuntimeChangeSignalCoalesces(t *testing.T) {- s := &EnvironmentService{}+ s := NewEnvironmentService(nil, http.DefaultClient, nil, nil, nil, nil)changes, unsubscribe := s.SubscribeRuntimeChanges()defer unsubscribe()diff --git a/backend/internal/job/service_test.go b/backend/internal/job/service_test.goindex 6647bc734..3fb263935 100644--- a/backend/internal/job/service_test.go+++ b/backend/internal/job/service_test.go@@ -36,6 +36,20 @@ import (francistest "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/francis/testing")+type fakeJobScheduler struct {+ jobs map[string]scheduler.Job+ runtimeStates map[string]scheduler.JobRuntimeState+ rescheduled []string+ rescheduleContexts []context.Context+ rescheduleErr error+ busWatcherRuns []string+ busWatcherContexts []context.Context+}++type fakeJob struct {+ name string+}+func setupSettingsTestDB(t *testing.T) *database.DB {t.Helper()db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})@@ -78,7 +92,7 @@ func newJobServiceForTest(t *testing.T, db *database.DB, settingsService *settincfg,newJobCoordinatorForTest(t, db),role.NewRoleService(db),- environment.NewEnvironmentService(db, nil, nil, nil, nil, nil),+ environment.NewEnvironmentService(db, http.DefaultClient, nil, nil, settingsService, nil),activity.NewActivityService(db, settingsService),)service.SetScheduler(t.Context(), newFakeJobScheduler())@@ -551,16 +565,6 @@ func findJobStatusByID(t *testing.T, jobs []jobschedule.JobStatus, id string) joreturn jobschedule.JobStatus{}}-type fakeJobScheduler struct {- jobs map[string]scheduler.Job- runtimeStates map[string]scheduler.JobRuntimeState- rescheduled []string- rescheduleContexts []context.Context- rescheduleErr error- busWatcherRuns []string- busWatcherContexts []context.Context-}-func newFakeJobScheduler(jobIDs ...string) *fakeJobScheduler {jobs := make(map[string]scheduler.Job, len(jobIDs))for _, jobID := range jobIDs {@@ -603,10 +607,6 @@ func (s *fakeJobScheduler) RunBusWatcherNow(ctx context.Context, watcherID strinreturn nil}-type fakeJob struct {- name string-}-func (j fakeJob) Name() string {return j.name}@@ -634,7 +634,7 @@ func newJobActivityTestService(t *testing.T) (*JobService, *activity.ActivitySer&config.Config{},newJobCoordinatorForTest(t, db),role.NewRoleService(db),- environment.NewEnvironmentService(db, nil, nil, nil, nil, nil),+ environment.NewEnvironmentService(db, http.DefaultClient, nil, nil, settingsService, nil),activities,)svc.SetScheduler(t.Context(), newFakeJobScheduler())@@ -939,7 +939,7 @@ func TestJobActivityRestartRepairsFailedProjection(t *testing.T) {&config.Config{},svc.runs,role.NewRoleService(db),- environment.NewEnvironmentService(db, nil, nil, nil, nil, nil),+ environment.NewEnvironmentService(db, http.DefaultClient, nil, nil, svc.settings, nil),activities,)restarted.SetScheduler(ctx, newFakeJobScheduler())@@ -1134,9 +1134,9 @@ func TestResolveRemoteRunConfirmsOwnerAfterLostResponse(t *testing.T) {ApiUrl: server.URL,Enabled: true,}).Error)- environments := environment.NewEnvironmentService(db, server.Client(), nil, nil, nil, nil)settingsService, err := newSettingsServiceForTest(t, t.Context(), db)require.NoError(t, err)+ environments := environment.NewEnvironmentService(db, server.Client(), nil, nil, settingsService, nil)svc := NewJobService(db,settingsService,diff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.goindex bae03e85e..e11a762b7 100644--- a/backend/internal/network/handler.go+++ b/backend/internal/network/handler.go@@ -3,8 +3,7 @@ package networkimport ("cmp""context"- "errors"- "sort"+ "slices""strings""github.com/danielgtaylor/huma/v2"@@ -147,7 +146,7 @@ func (h *NetworkHandler) CreateNetwork(ctx context.Context, input *CreateNetworkreturn nil, huma.Error400BadRequest("Invalid network options: " + err.Error())}if !input.Body.Options.EnableIPv6 {- dockerOptions.EnableIPv6 = nil+ dockerOptions.EnableIPv6 = nil // keep: mapped options need nil to omit IPv6}var response *network.CreateResponse@@ -179,7 +178,7 @@ func (h *NetworkHandler) CreateNetwork(ctx context.Context, input *CreateNetworkif err != nil {return nil, huma.Error500InternalServerError("Failed to map network: " + err.Error())}- out.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer()+ out.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer() // keep: enriches the mapped DTOreturn &handlerutil.Out[networktypes.CreateResponse]{Body: base.ApiResponse[networktypes.CreateResponse]{@@ -201,49 +200,31 @@ func (h *NetworkHandler) GetNetwork(ctx context.Context, input *GetNetworkInput)}// Ensure ID is mapped correctly- if out.ID == "" {- out.ID = networkInspect.ID- }+ out.ID = cmp.Or(out.ID, networkInspect.ID) // keep: enriches the mapped DTO// Populate ContainersList- out.ContainersList = make([]networktypes.ContainerEndpoint, 0, len(out.Containers))+ containers := make([]networktypes.ContainerEndpoint, 0, len(out.Containers))for id, container := range out.Containers {- ipv4Address := ""- if container.IPv4Address.IsValid() {- ipv4Address = container.IPv4Address.String()- }- ipv6Address := ""- if container.IPv6Address.IsValid() {- ipv6Address = container.IPv6Address.String()- }- out.ContainersList = append(out.ContainersList, networktypes.ContainerEndpoint{+ containers = append(containers, networktypes.ContainerEndpoint{ID: id,Name: container.Name,EndpointID: container.EndpointID,- IPv4Address: ipv4Address,- IPv6Address: ipv6Address,+ IPv4Address: kit.Ternary(container.IPv4Address.IsValid(), container.IPv4Address.String(), ""),+ IPv6Address: kit.Ternary(container.IPv6Address.IsValid(), container.IPv6Address.String(), ""),MacAddress: container.MacAddress.String(),})}// Sort ContainersList- sort.Slice(out.ContainersList, func(i, j int) bool {- a, b := out.ContainersList[i], out.ContainersList[j]-+ slices.SortFunc(containers, func(a, b networktypes.ContainerEndpoint) int {if input.Sort == "ip" {- valA := cmp.Or(a.IPv4Address, a.IPv6Address)- valB := cmp.Or(b.IPv4Address, b.IPv6Address)-- localCmp := kit.CompareAddresses(valA, valB)- return kit.Ternary(input.Order == "desc", localCmp > 0, localCmp < 0)+ return kit.Ternary(input.Order == "desc", -1, 1) * kit.CompareAddresses(cmp.Or(a.IPv4Address, a.IPv6Address), cmp.Or(b.IPv4Address, b.IPv6Address))}// Default to Name- if input.Order == "desc" {- return strings.ToLower(a.Name) > strings.ToLower(b.Name)- }- return strings.ToLower(a.Name) < strings.ToLower(b.Name)+ return kit.Ternary(input.Order == "desc", -1, 1) * strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))})+ out.ContainersList = containers // keep: enriches the mapped DTOreturn &handlerutil.Out[networktypes.Inspect]{Body: base.ApiResponse[networktypes.Inspect]{@@ -322,10 +303,8 @@ func (h *NetworkHandler) ConnectContainer(ctx context.Context, input *ConnectConreturn h.networkService.ConnectContainer(runtimeCtx, input.NetworkID, input.Body, *user)})if err != nil {- if errors.Is(err, common.ErrValidation) {- return nil, huma.Error400BadRequest(err.Error())- }- return nil, huma.Error500InternalServerError("Failed to connect container to network: " + err.Error())+ apiErr := common.ToAPIError(err)+ return nil, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)}return handlerutil.MessageOutput("Container connected successfully", activityID), nil@@ -387,7 +366,7 @@ func (h *NetworkHandler) PruneNetworks(ctx context.Context, input *PruneNetworksif err != nil {return nil, huma.Error500InternalServerError("Failed to map network: " + err.Error())}- out.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer()+ out.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer() // keep: enriches the mapped DTOreturn &handlerutil.Out[networktypes.PruneReport]{Body: base.ApiResponse[networktypes.PruneReport]{diff --git a/backend/internal/network/service.go b/backend/internal/network/service.goindex 3b5a5d8c0..9050172e5 100644--- a/backend/internal/network/service.go+++ b/backend/internal/network/service.go@@ -8,7 +8,6 @@ import ("net/netip""net/url""slices"- "sort""strings""github.com/getarcaneapp/arcane/types/v2/base"@@ -72,43 +71,31 @@ func (s *NetworkService) GetNetworkTopology(ctx context.Context) (*networktypes.return nil, fmt.Errorf("failed to list containers: %w", err)}- containerInfoByID := buildTopologyContainerInfoInternal(containers)+ containersByID := make(map[string]container.Summary, len(containers))+ for _, rawContainer := range containers {+ containersByID[rawContainer.ID] = rawContainer+ }networkList, err := compat.NetworkListWithCompatibility(ctx, dockerClient, client.NetworkListOptions{})if err != nil {return nil, fmt.Errorf("failed to list Docker networks: %w", err)}- topology := &networktypes.Topology{- Nodes: make([]networktypes.TopologyNode, 0, len(networkList.Items)),- Edges: make([]networktypes.TopologyEdge, 0),- }- containerNodeIDs := make(map[string]struct{})- inspectedNetworks := make([]struct {- summary network.Summary- inspect network.Inspect- }, len(networkList.Items))+ inspectedNetworks := make([]network.Inspect, len(networkList.Items))g, groupCtx := errgroup.WithContext(ctx)g.SetLimit(8)- for i := range networkList.Items {- rawNetwork := networkList.Items[i]+ for i, rawNetwork := range networkList.Items {g.Go(func() (workerErr error) {defer utils.RecoverToError(&workerErr, "network worker")- inspected, networkInspectWithCompatibilityErr := compat.NetworkInspectWithCompatibility(groupCtx, dockerClient, rawNetwork.ID, client.NetworkInspectOptions{})- if networkInspectWithCompatibilityErr != nil {- return fmt.Errorf("failed to inspect network %s: %w", rawNetwork.Name, networkInspectWithCompatibilityErr)+ inspected, inspectErr := compat.NetworkInspectWithCompatibility(groupCtx, dockerClient, rawNetwork.ID, client.NetworkInspectOptions{})+ if inspectErr != nil {+ return fmt.Errorf("failed to inspect network %s: %w", rawNetwork.Name, inspectErr)}- inspectedNetworks[i] = struct {- summary network.Summary- inspect network.Inspect- }{- summary: rawNetwork,- inspect: inspected.Network,- }+ inspectedNetworks[i] = inspected.Networkreturn nil})}@@ -117,9 +104,11 @@ func (s *NetworkService) GetNetworkTopology(ctx context.Context) (*networktypes.return nil, waitErr}- for _, inspectedNetwork := range inspectedNetworks {- rawNetwork := inspectedNetwork.summary- topology.Nodes = append(topology.Nodes, networktypes.TopologyNode{+ nodes := make([]networktypes.TopologyNode, 0, len(networkList.Items))+ edges := make([]networktypes.TopologyEdge, 0)+ containerNodeIDs := make(map[string]struct{})+ for i, rawNetwork := range networkList.Items {+ nodes = append(nodes, networktypes.TopologyNode{ID: rawNetwork.ID,Name: rawNetwork.Name,Type: networktypes.TopologyNodeTypeNetwork,@@ -130,55 +119,42 @@ func (s *NetworkService) GetNetworkTopology(ctx context.Context) (*networktypes.},})- for containerID, endpoint := range inspectedNetwork.inspect.Containers {- info, ok := containerInfoByID[containerID]- if !ok {- info = topologyContainerInfo{Name: endpoint.Name}- }- info.Name = cmp.Or(info.Name, endpoint.Name)-+ for containerID, endpoint := range inspectedNetworks[i].Containers {if _, exists := containerNodeIDs[containerID]; !exists {- topology.Nodes = append(topology.Nodes, networktypes.TopologyNode{+ ctr := containersByID[containerID]+ nodes = append(nodes, networktypes.TopologyNode{ID: containerID,- Name: info.Name,+ Name: cmp.Or(kit.Ternary(len(ctr.Names) > 0, docker.ContainerNameFromNames(ctr.Names), ctr.ID), endpoint.Name),Type: networktypes.TopologyNodeTypeContainer,Metadata: networktypes.TopologyNodeMetadata{- Status: info.State,- Image: info.Image,+ Status: string(ctr.State),+ Image: ctr.Image,},})containerNodeIDs[containerID] = struct{}{}}- edge := networktypes.TopologyEdge{- ID: fmt.Sprintf("%s:%s", rawNetwork.ID, containerID),- Source: rawNetwork.ID,- Target: containerID,- }- if endpoint.IPv4Address.IsValid() {- edge.IPv4Address = endpoint.IPv4Address.String()- }- if endpoint.IPv6Address.IsValid() {- edge.IPv6Address = endpoint.IPv6Address.String()- }-- topology.Edges = append(topology.Edges, edge)+ edges = append(edges, networktypes.TopologyEdge{+ ID: fmt.Sprintf("%s:%s", rawNetwork.ID, containerID),+ Source: rawNetwork.ID,+ Target: containerID,+ IPv4Address: kit.Ternary(endpoint.IPv4Address.IsValid(), endpoint.IPv4Address.String(), ""),+ IPv6Address: kit.Ternary(endpoint.IPv6Address.IsValid(), endpoint.IPv6Address.String(), ""),+ })}}- sort.Slice(topology.Nodes, func(i, j int) bool {- left, right := topology.Nodes[i], topology.Nodes[j]- if left.Type != right.Type {- return left.Type < right.Type- }- return strings.ToLower(left.Name) < strings.ToLower(right.Name)+ slices.SortFunc(nodes, func(a, b networktypes.TopologyNode) int {+ return cmp.Or(cmp.Compare(a.Type, b.Type), strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)))})- sort.Slice(topology.Edges, func(i, j int) bool {- left, right := topology.Edges[i], topology.Edges[j]- return kit.Ternary(left.Source != right.Source, left.Source < right.Source, left.Target < right.Target)+ slices.SortFunc(edges, func(a, b networktypes.TopologyEdge) int {+ return cmp.Or(strings.Compare(a.Source, b.Source), strings.Compare(a.Target, b.Target))})- return topology, nil+ return &networktypes.Topology{+ Nodes: nodes,+ Edges: edges,+ }, nil}func (s *NetworkService) CreateNetwork(ctx context.Context, name string, options client.NetworkCreateOptions, user usertypes.Actor) (*network.CreateResponse, error) {@@ -195,26 +171,18 @@ func (s *NetworkService) CreateNetwork(ctx context.Context, name string, optionsreturn nil, fmt.Errorf("failed to create network: %w", err)}- metadata := database.JSON{+ if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkCreate, response.ID, name, user.ID, user.Username, "0", database.JSON{"action": "create","driver": options.Driver,"name": name,- }- if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkCreate, response.ID, name, user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log network creation action", "error", logErr)}- warning := ""- if len(response.Warning) > 0 {- warning = strings.Join(response.Warning, "; ")- }-- out := network.CreateResponse{+ return &network.CreateResponse{ID: response.ID,- Warning: warning,- }-- return &out, nil+ Warning: strings.Join(response.Warning, "; "),+ }, nil}func (s *NetworkService) RemoveNetwork(ctx context.Context, id string, user usertypes.Actor) error {@@ -233,11 +201,10 @@ func (s *NetworkService) RemoveNetwork(ctx context.Context, id string, user userreturn fmt.Errorf("failed to remove network: %w", networkRemoveErr)}- metadata := database.JSON{+ if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, id, networkName, user.ID, user.Username, "0", database.JSON{"action": "delete","networkId": id,- }- if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, id, networkName, user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log network delete action", "error", logErr)}@@ -252,31 +219,29 @@ func (s *NetworkService) ConnectContainer(ctx context.Context, networkID string,return fmt.Errorf("failed to connect to Docker: %w", err)}- endpoint := &network.EndpointSettings{Aliases: req.Aliases}- ipv4 := strings.TrimSpace(req.IPv4Address)- ipv6 := strings.TrimSpace(req.IPv6Address)- if ipv4 != "" || ipv6 != "" {- ipam := &network.EndpointIPAMConfig{}- if ipv4 != "" {- addr, parseErr := netip.ParseAddr(ipv4)- if parseErr != nil {- return common.Classify(common.ErrValidation, fmt.Errorf("invalid IPv4 address %s: %w", ipv4, parseErr))- }- ipam.IPv4Address = addr- }- if ipv6 != "" {- addr, parseErr := netip.ParseAddr(ipv6)- if parseErr != nil {- return common.Classify(common.ErrValidation, fmt.Errorf("invalid IPv6 address %s: %w", ipv6, parseErr))- }- ipam.IPv6Address = addr- }- endpoint.IPAMConfig = ipam+ ipv4, ipv4Err := netip.ParseAddr(strings.TrimSpace(req.IPv4Address))+ if ipv4Err != nil && strings.TrimSpace(req.IPv4Address) != "" {+ return common.Classify(common.ErrValidation, fmt.Errorf("invalid IPv4 address %s: %w", strings.TrimSpace(req.IPv4Address), ipv4Err))+ }+ ipv6, ipv6Err := netip.ParseAddr(strings.TrimSpace(req.IPv6Address))+ if ipv6Err != nil && strings.TrimSpace(req.IPv6Address) != "" {+ return common.Classify(common.ErrValidation, fmt.Errorf("invalid IPv6 address %s: %w", strings.TrimSpace(req.IPv6Address), ipv6Err))}if _, networkConnectErr := dockerClient.NetworkConnect(ctx, networkID, client.NetworkConnectOptions{- Container: req.ContainerID,- EndpointConfig: endpoint,+ Container: req.ContainerID,+ EndpointConfig: &network.EndpointSettings{+ Aliases: req.Aliases,+ IPAMConfig: func() *network.EndpointIPAMConfig {+ if !ipv4.IsValid() && !ipv6.IsValid() {+ return nil+ }+ return &network.EndpointIPAMConfig{+ IPv4Address: ipv4,+ IPv6Address: ipv6,+ }+ }(),+ },}); networkConnectErr != nil {s.eventService.LogErrorEvent(ctx,@@ -296,12 +261,11 @@ func (s *NetworkService) ConnectContainer(ctx context.Context, networkID string,return fmt.Errorf("failed to connect container to network: %w", networkConnectErr)}- metadata := database.JSON{+ if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkConnect, networkID, networkID, user.ID, user.Username, "0", database.JSON{"action": "connect","networkId": networkID,"containerId": req.ContainerID,- }- if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkConnect, networkID, networkID, user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log network connect action", "error", logErr)}@@ -338,13 +302,12 @@ func (s *NetworkService) DisconnectContainer(ctx context.Context, networkID strireturn fmt.Errorf("failed to disconnect container from network: %w", networkDisconnectErr)}- metadata := database.JSON{+ if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDisconnect, networkID, networkID, user.ID, user.Username, "0", database.JSON{"action": "disconnect","networkId": networkID,"containerId": req.ContainerID,"force": req.Force,- }- if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDisconnect, networkID, networkID, user.ID, user.Username, "0", metadata); logErr != nil {+ }); logErr != nil {slog.WarnContext(ctx, "could not log network disconnect action", "error", logErr)}@@ -357,23 +320,19 @@ func (s *NetworkService) PruneNetworks(ctx context.Context) (*network.PruneReporreturn nil, fmt.Errorf("failed to connect to Docker: %w", err)}- filterArgs := make(client.Filters)-- report, err := dockerClient.NetworkPrune(ctx, client.NetworkPruneOptions{Filters: filterArgs})+ report, err := dockerClient.NetworkPrune(ctx, client.NetworkPruneOptions{Filters: make(client.Filters)})if err != nil {return nil, fmt.Errorf("failed to prune networks: %w", err)}- pruneReport := report.Report- metadata := database.JSON{+ if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, "", "bulk_prune", usertypes.SystemUser.ID, usertypes.SystemUser.Username, "0", database.JSON{"action": "prune",- "networksDeleted": len(pruneReport.NetworksDeleted),- }- if logErr := s.eventService.LogNetworkEvent(ctx, event.EventTypeNetworkDelete, "", "bulk_prune", usertypes.SystemUser.ID, usertypes.SystemUser.Username, "0", metadata); logErr != nil {+ "networksDeleted": len(report.Report.NetworksDeleted),+ }); logErr != nil {slog.WarnContext(ctx, "could not log network prune action", "error", logErr)}- return &pruneReport, nil+ return &report.Report, nil}func (s *NetworkService) ListNetworksPaginated(ctx context.Context, params pagination.QueryParams) ([]networktypes.Summary, pagination.Response, networktypes.UsageCounts, error) {@@ -393,54 +352,30 @@ func (s *NetworkService) ListNetworksPaginated(ctx context.Context, params paginif err != nil {return nil, pagination.Response{}, networktypes.UsageCounts{}, fmt.Errorf("failed to list Docker networks: %w", err)}- rawNets := networkList.Items-- items, err := s.convertToNetworkSummaries(rawNets, inUseByID, inUseByName)- if err != nil {- return nil, pagination.Response{}, networktypes.UsageCounts{}, err- }- config := s.buildNetworkPaginationConfig()- result := config.SearchOrderAndPaginate(items, params)- counts := s.calculateNetworkUsageCounts(items)- paginationResp := pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params)-- return result.Items, paginationResp, counts, nil-}--type topologyContainerInfo struct {- Name string- Image string- State string-}-func buildTopologyContainerInfoInternal(containers []container.Summary) map[string]topologyContainerInfo {- infoByID := make(map[string]topologyContainerInfo, len(containers))- for _, rawContainer := range containers {- name := rawContainer.ID- if len(rawContainer.Names) > 0 {- name = docker.ContainerNameFromNames(rawContainer.Names)+ items := make([]networktypes.Summary, 0, len(networkList.Items))+ inUse := 0+ for _, n := range networkList.Items {+ netDto, mapErr := mapping.MapOne[network.Summary, networktypes.Summary](n)+ if mapErr != nil {+ return nil, pagination.Response{}, networktypes.UsageCounts{}, fmt.Errorf("failed to map network: %w", mapErr)}- infoByID[rawContainer.ID] = topologyContainerInfo{- Name: name,- Image: rawContainer.Image,- State: string(rawContainer.State),+ netDto.InUse = inUseByID[netDto.ID] || inUseByName[netDto.Name] // keep: enriches the mapped DTO+ netDto.IsDefault = docker.IsDefaultNetwork(netDto.Name) // keep: enriches the mapped DTO+ items = append(items, netDto)+ if netDto.InUse || netDto.IsDefault {+ inUse++}}- return infoByID-}+ result := s.buildNetworkPaginationConfig().SearchOrderAndPaginate(items, params)-func (s *NetworkService) convertToNetworkSummaries(rawNets []network.Summary, inUseByID, inUseByName map[string]bool) ([]networktypes.Summary, error) {- items := make([]networktypes.Summary, 0, len(rawNets))- for _, n := range rawNets {- netDto, err := mapping.MapOne[network.Summary, networktypes.Summary](n)- if err != nil {- return nil, fmt.Errorf("failed to map network: %w", err)- }- netDto.InUse = inUseByID[netDto.ID] || inUseByName[netDto.Name]- netDto.IsDefault = docker.IsDefaultNetwork(netDto.Name)- items = append(items, netDto)- }- return items, nil+ return result.Items, pagination.BuildResponse(result.TotalCount, result.TotalAvailable, params), networktypes.UsageCounts{+ Inuse: inUse,+ // Only count non-default networks as unused+ // Default networks (bridge, host, none, ingress) are never "unused"+ Unused: len(items) - inUse,+ Total: len(items),+ }, nil}func (s *NetworkService) AggregateNetworks(ctx context.Context, params pagination.QueryParams) (base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts], error) {@@ -450,24 +385,27 @@ func (s *NetworkService) AggregateNetworks(ctx context.Context, params paginatio}pages := make([]pagination.EnvironmentPage[networktypes.Summary], 0, len(collection.Results))- counts := networktypes.UsageCounts{}+ inUse, unused, total := 0, 0, 0for _, result := range collection.Results {- page := result.Valuepages = append(pages, pagination.EnvironmentPage[networktypes.Summary]{Environment: result.Environment,- Items: page.Data,- TotalAvailable: cmp.Or(page.Pagination.GrandTotalItems, page.Pagination.TotalItems),+ Items: result.Value.Data,+ TotalAvailable: cmp.Or(result.Value.Pagination.GrandTotalItems, result.Value.Pagination.TotalItems),})- counts.Inuse += page.Counts.Inuse- counts.Unused += page.Counts.Unused- counts.Total += page.Counts.Total+ inUse += result.Value.Counts.Inuse+ unused += result.Value.Counts.Unused+ total += result.Value.Counts.Total}data, resp := pagination.MergeEnvironmentPages(s.buildNetworkPaginationConfig(), pages, params)return base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{- Success: true,- Data: data,- Counts: counts,+ Success: true,+ Data: data,+ Counts: networktypes.UsageCounts{+ Inuse: inUse,+ Unused: unused,+ Total: total,+ },Pagination: handlerutil.PaginationResponse(resp),FailedEnvironments: collection.Failures,}, nil@@ -481,64 +419,80 @@ func (s *NetworkService) buildNetworkPaginationConfig() pagination.Config[networfunc(n networktypes.Summary) (string, error) { return n.Scope, nil },func(n networktypes.Summary) (string, error) { return n.ID, nil },func(n networktypes.Summary) (string, error) {- subnets, gateways := ipamValuesInternal(n)+ subnets, gateways := ipamValues(n)return strings.Join(append(subnets, gateways...), " "), nil},},- SortBindings: s.buildNetworkSortBindings(),- FilterAccessors: s.buildNetworkFilterAccessors(),- }-}--func (s *NetworkService) buildNetworkSortBindings() []pagination.SortBinding[networktypes.Summary] {- return []pagination.SortBinding[networktypes.Summary]{- {- Key: "name",- // Docker allows duplicate network names, so tie-break on ID to keep- // pagination deterministic.- Fn: func(a, b networktypes.Summary) int {- if c := strings.Compare(a.Name, b.Name); c != 0 {- return c- }- return strings.Compare(a.ID, b.ID)+ SortBindings: []pagination.SortBinding[networktypes.Summary]{+ {+ Key: "name",+ // Docker allows duplicate network names, so tie-break on ID to keep+ // pagination deterministic.+ Fn: func(a, b networktypes.Summary) int {+ return cmp.Or(strings.Compare(a.Name, b.Name), strings.Compare(a.ID, b.ID))+ },},- },- {- Key: "driver",- Fn: func(a, b networktypes.Summary) int { return strings.Compare(a.Driver, b.Driver) },- },- {- Key: "scope",- Fn: func(a, b networktypes.Summary) int { return strings.Compare(a.Scope, b.Scope) },- },- {- Key: "created",- Fn: s.compareNetworkCreated,- },- {- Key: "inUse",- Fn: s.compareNetworkInUse,- },- {- Key: "subnet",- Fn: func(a, b networktypes.Summary) int {- subnetsA, _ := ipamValuesInternal(a)- subnetsB, _ := ipamValuesInternal(b)- return compareIPAMListsInternal(subnetsA, subnetsB, kit.CompareSubnets)+ {+ Key: "driver",+ Fn: func(a, b networktypes.Summary) int { return strings.Compare(a.Driver, b.Driver) },+ },+ {+ Key: "scope",+ Fn: func(a, b networktypes.Summary) int { return strings.Compare(a.Scope, b.Scope) },+ },+ {+ Key: "created",+ Fn: func(a, b networktypes.Summary) int { return a.Created.Compare(b.Created) },+ },+ {+ Key: "inUse",+ Fn: func(a, b networktypes.Summary) int {+ if (a.InUse || a.IsDefault) == (b.InUse || b.IsDefault) {+ // Use name as secondary sort key for consistent ordering+ return strings.Compare(a.Name, b.Name)+ }+ return kit.Ternary(a.InUse || a.IsDefault, -1, 1)+ },+ },+ // Subnet and gateway compare sorted copies so IPAM entry order does+ // not affect row order; the displayed entries are untouched.+ {+ Key: "subnet",+ Fn: func(a, b networktypes.Summary) int {+ subnetsA, _ := ipamValues(a)+ subnetsB, _ := ipamValues(b)+ return slices.CompareFunc(+ slices.SortedFunc(slices.Values(subnetsA), kit.CompareSubnets),+ slices.SortedFunc(slices.Values(subnetsB), kit.CompareSubnets),+ kit.CompareSubnets,+ )+ },+ },+ {+ Key: "gateway",+ Fn: func(a, b networktypes.Summary) int {+ _, gatewaysA := ipamValues(a)+ _, gatewaysB := ipamValues(b)+ return slices.CompareFunc(+ slices.SortedFunc(slices.Values(gatewaysA), kit.CompareAddresses),+ slices.SortedFunc(slices.Values(gatewaysB), kit.CompareAddresses),+ kit.CompareAddresses,+ )+ },},},- {- Key: "gateway",- Fn: func(a, b networktypes.Summary) int {- _, gatewaysA := ipamValuesInternal(a)- _, gatewaysB := ipamValuesInternal(b)- return compareIPAMListsInternal(gatewaysA, gatewaysB, kit.CompareAddresses)+ FilterAccessors: []pagination.FilterAccessor[networktypes.Summary]{+ {+ Key: "inUse",+ Fn: func(n networktypes.Summary, filterValue string) bool {+ return kit.Ternary(filterValue == "true", n.InUse || n.IsDefault, filterValue != "false" || (!n.InUse && !n.IsDefault))+ },},},}}-func ipamValuesInternal(n networktypes.Summary) (subnets, gateways []string) {+func ipamValues(n networktypes.Summary) (subnets, gateways []string) {for _, cfg := range n.IPAM.Config {if cfg.Subnet != "" {subnets = append(subnets, cfg.Subnet)@@ -549,56 +503,3 @@ func ipamValuesInternal(n networktypes.Summary) (subnets, gateways []string) {}return subnets, gateways}--// compareIPAMListsInternal sorts copies of both lists so IPAM entry order does-// not affect row order; the displayed entries are untouched.-func compareIPAMListsInternal(a, b []string, compare func(x, y string) int) int {- sortedA := slices.SortedFunc(slices.Values(a), compare)- sortedB := slices.SortedFunc(slices.Values(b), compare)- return slices.CompareFunc(sortedA, sortedB, compare)-}--func (s *NetworkService) compareNetworkCreated(a, b networktypes.Summary) int {- if a.Created.Before(b.Created) {- return -1- }- return kit.Ternary(a.Created.After(b.Created), 1, 0)-}--func (s *NetworkService) compareNetworkInUse(a, b networktypes.Summary) int {- aInUse := a.InUse || a.IsDefault- bInUse := b.InUse || b.IsDefault-- if aInUse == bInUse {- // Use name as secondary sort key for consistent ordering- return strings.Compare(a.Name, b.Name)- }- return kit.Ternary(aInUse, -1, 1)-}--func (s *NetworkService) buildNetworkFilterAccessors() []pagination.FilterAccessor[networktypes.Summary] {- return []pagination.FilterAccessor[networktypes.Summary]{- {- Key: "inUse",- Fn: func(n networktypes.Summary, filterValue string) bool {- return kit.Ternary(filterValue == "true", n.InUse || n.IsDefault, filterValue != "false" || (!n.InUse && !n.IsDefault))- },- },- }-}--func (s *NetworkService) calculateNetworkUsageCounts(items []networktypes.Summary) networktypes.UsageCounts {- counts := networktypes.UsageCounts{- Total: len(items),- }- for _, n := range items {- if n.InUse || n.IsDefault {- counts.Inuse++- } else {- // Only count non-default networks as unused- // Default networks (bridge, host, none, ingress) are never "unused"- counts.Unused++- }- }- return counts-}diff --git a/backend/internal/network/service_test.go b/backend/internal/network/service_test.goindex 011b24c28..f78579cba 100644--- a/backend/internal/network/service_test.go+++ b/backend/internal/network/service_test.go@@ -43,14 +43,40 @@ func networkScopeContext(t *testing.T, target string, bodies map[string]any) (cofunc TestAggregateNetworksMergesEnvironments(t *testing.T) {bodies := map[string]any{- "a": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{Success: true, Data: []networktypes.Summary{- {ID: "a1", Name: "bridge", InUse: true},- {ID: "a2", Name: "frontend"},- }, Counts: networktypes.UsageCounts{Inuse: 1, Unused: 1, Total: 2}, Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3}},- "b": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{Success: true, Data: []networktypes.Summary{- {ID: "b1", Name: "backend", InUse: true},- {ID: "b2", Name: "bridge", InUse: true},- }, Counts: networktypes.UsageCounts{Inuse: 2, Total: 2}, Pagination: base.PaginationResponse{TotalItems: 2}},+ "a": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{+ Success: true,+ Data: []networktypes.Summary{+ {+ ID: "a1",+ Name: "bridge",+ InUse: true,+ },+ {ID: "a2", Name: "frontend"},+ },+ Counts: networktypes.UsageCounts{+ Inuse: 1,+ Unused: 1,+ Total: 2,+ },+ Pagination: base.PaginationResponse{TotalItems: 2, GrandTotalItems: 3},+ },+ "b": base.PaginatedWithCounts[networktypes.Summary, networktypes.UsageCounts]{+ Success: true,+ Data: []networktypes.Summary{+ {+ ID: "b1",+ Name: "backend",+ InUse: true,+ },+ {+ ID: "b2",+ Name: "bridge",+ InUse: true,+ },+ },+ Counts: networktypes.UsageCounts{Inuse: 2, Total: 2},+ Pagination: base.PaginationResponse{TotalItems: 2},+ },}rowLabels := func(items []networktypes.Summary) []string {out := make([]string, 0, len(items))@@ -73,7 +99,11 @@ func TestAggregateNetworksMergesEnvironments(t *testing.T) {require.Equal(t, int64(5), body.Pagination.GrandTotalItems)require.Equal(t, int64(2), body.Pagination.TotalPages)require.Equal(t, 2, body.Pagination.CurrentPage)- require.Equal(t, networktypes.UsageCounts{Inuse: 3, Unused: 1, Total: 4}, body.Counts)+ require.Equal(t, networktypes.UsageCounts{+ Inuse: 3,+ Unused: 1,+ Total: 4,+ }, body.Counts)require.Equal(t, []base.EnvironmentFailure{{Environment: base.EnvironmentRef{ID: "c", Name: "c"}, Message: "environment offline"}}, body.FailedEnvironments)for _, id := range []string{"a", "b"} {stored, ok := queries.Load(id)