Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: stabilize container redeploys, exec sessions, and streams (#4488)
Kyle Mendella7812907535f
diff --git a/backend/api/ws/container_stats.go b/backend/api/ws/container_stats.goindex d6b360902..d75c90e01 100644--- a/backend/api/ws/container_stats.go+++ b/backend/api/ws/container_stats.go@@ -63,9 +63,7 @@ func (h *WebSocketHandler) ContainerStats(c *echo.Context) error {return nil}-// containerStatsErrorDrainGrace is how long a failed stats hub stays alive after-// broadcasting its error frame, so clients receive it before the hub shuts down.-const containerStatsErrorDrainGrace = 2 * time.Second+const streamEndDrainGrace = 2 * time.Secondfunc (h *WebSocketHandler) runContainerStatsHub(containerID string, hub *wshub.Hub) {ctx, cancel := context.WithCancel(h.ctx)@@ -81,7 +79,7 @@ func (h *WebSocketHandler) runContainerStatsHub(containerID string, hub *wshub.Htimer = time.AfterFunc(5*time.Second, func() {cleanupTimerMu.Lock()defer cleanupTimerMu.Unlock()- if cleanupTimer != timer {+ if cleanupTimer != timer || hub.Len() > 0 {return}h.containerStatsHubs.CompareAndDelete(containerID, hub)@@ -129,7 +127,7 @@ func (h *WebSocketHandler) runContainerStatsHub(containerID string, hub *wshub.H// Cancelling immediately would race hub.Run between ctx.Done and the// queued error frame, dropping the very message clients need. Give the// broadcast a moment to drain, then tear the hub down.- time.AfterFunc(containerStatsErrorDrainGrace, cancel)+ time.AfterFunc(streamEndDrainGrace, cancel)}(ctx)go func() {diff --git a/backend/api/ws/diagnostics.go b/backend/api/ws/diagnostics.goindex f27812cf8..3b6d8b758 100644--- a/backend/api/ws/diagnostics.go+++ b/backend/api/ws/diagnostics.go@@ -155,12 +155,31 @@ func (h *WebSocketHandler) streamSnapshots(c *echo.Context, interval time.Durati}var onMessage func([]byte)if handle != nil {+ commands := make(chan systemtypes.DiagnosticsCommand, 1)+ go func() {+ for {+ select {+ case <-ctx.Done():+ return+ case cmd := <-commands:+ write(handle(ctx, cmd))+ }+ }+ }()onMessage = func(b []byte) {var cmd systemtypes.DiagnosticsCommandif json.Unmarshal(b, &cmd) != nil {return}- go write(handle(ctx, cmd))+ select {+ case commands <- cmd:+ default:+ write(systemtypes.DiagnosticsMessage{+ Type: "result",+ ID: cmd.ID,+ Error: "busy",+ })+ }}}done := diagnosticsReadLoop(ctx, conn, onMessage)diff --git a/backend/api/ws/exec.go b/backend/api/ws/exec.goindex dc6be8520..e0f2420b4 100644--- a/backend/api/ws/exec.go+++ b/backend/api/ws/exec.go@@ -3,11 +3,13 @@ package wsimport ("cmp""context"+ "encoding/json/v2""errors""fmt""io""log/slog""net/http"+ "strconv""strings""sync""time"@@ -39,6 +41,8 @@ func (h *WebSocketHandler) ContainerExec(c *echo.Context) error {}shell := cmp.Or(c.QueryParam("shell"), "/bin/sh")+ cols, _ := strconv.ParseUint(c.QueryParam("cols"), 10, 16)+ rows, _ := strconv.ParseUint(c.QueryParam("rows"), 10, 16)conn, unregister, ok := h.acceptWS(c, systemtypes.WSKindContainerExec, containerID)if !ok {@@ -56,14 +60,14 @@ func (h *WebSocketHandler) ContainerExec(c *echo.Context) error {// The pong is serviced by the concurrent stdin reader.go wshub.KeepAlive(ctx, func() { cancel(errors.New("websocket ping failed")) }, conn)- h.runContainerExec(ctx, cancel, conn, containerID, shell)+ h.runContainerExec(ctx, cancel, conn, containerID, shell, uint(cols), uint(rows))return nil}-func (h *WebSocketHandler) runContainerExec(ctx context.Context, cancel context.CancelCauseFunc, conn *websocket.Conn, containerID, shell string) {+func (h *WebSocketHandler) runContainerExec(ctx context.Context, cancel context.CancelCauseFunc, conn *websocket.Conn, containerID, shell string, cols, rows uint) {started := time.Now()- execID, err := h.containerService.CreateExec(ctx, containerID, []string{shell})+ execID, err := h.containerService.CreateExec(ctx, containerID, []string{shell}, cols, rows)if err != nil {h.writeExecError(ctx, conn, fmt.Errorf("Error creating exec: %w", err)) //nolint:staticcheck // Preserve the existing error message.closeExec(ctx, conn, containerID, "", started, fmt.Errorf("create exec: %w", err))@@ -95,7 +99,7 @@ func (h *WebSocketHandler) runContainerExec(ctx context.Context, cancel context.done := make(chan error, 1)go h.pipeExecOutput(ctx, conn, execSession.Stdout(), done)- go h.pipeExecInput(ctx, cancel, conn, execSession.Stdin())+ go h.pipeExecInput(ctx, cancel, conn, execID, execSession.Stdin())cause := <-doneif ctxCause := context.Cause(ctx); ctxCause != nil {@@ -167,13 +171,24 @@ func (h *WebSocketHandler) pipeExecOutput(ctx context.Context, conn *websocket.C}}-func (h *WebSocketHandler) pipeExecInput(ctx context.Context, cancel context.CancelCauseFunc, conn *websocket.Conn, stdin io.Writer) {+func (h *WebSocketHandler) pipeExecInput(ctx context.Context, cancel context.CancelCauseFunc, conn *websocket.Conn, execID string, stdin io.Writer) {for {- _, data, err := conn.Read(ctx)+ msgType, data, err := conn.Read(ctx)if err != nil {cancel(fmt.Errorf("websocket read: %w", err))return}+ var control struct {+ Type string `json:"type"`+ Cols uint `json:"cols"`+ Rows uint `json:"rows"`+ }+ if msgType == websocket.MessageText && json.Unmarshal(data, &control) == nil && control.Type == "resize" {+ if resizeErr := h.containerService.ResizeExec(ctx, execID, control.Cols, control.Rows); resizeErr != nil {+ slog.DebugContext(ctx, "Failed to resize exec", "execId", execID, "error", resizeErr)+ }+ continue+ }if _, writeErr := stdin.Write(data); writeErr != nil {cancel(fmt.Errorf("exec input write: %w", writeErr))returndiff --git a/backend/api/ws/log_streams.go b/backend/api/ws/log_streams.goindex 67db95130..2c245e8ec 100644--- a/backend/api/ws/log_streams.go+++ b/backend/api/ws/log_streams.go@@ -258,7 +258,7 @@ func (h *WebSocketHandler) startLogHub(slog.DebugContext(ctx, "client disconnected, cleaning up log hub", "kind", label, "resourceId", resourceID)})- lines := h.startLogSource(ctx, key, resourceID, label, params, stream, ls)+ lines := h.startLogSource(ctx, cancel, key, resourceID, label, params, stream, ls)startLogForwarders(ctx, ls, lines, params, normalizeJSON, normalizeText)return ls@@ -266,6 +266,7 @@ func (h *WebSocketHandler) startLogHub(func (h *WebSocketHandler) startLogSource(ctx context.Context,+ cancel context.CancelFunc,key, resourceID, label string,params logStreamParams,stream func(context.Context, string, chan<- string, bool, string, string, bool) error,@@ -288,11 +289,13 @@ func (h *WebSocketHandler) startLogSource(h.releaseLogStream(key, ls, true)broadcastLogStreamError(ctx, label, resourceID, params.format, err, ls)+ time.AfterFunc(streamEndDrainGrace, cancel)return}if ctx.Err() == nil {h.releaseLogStream(key, ls, true)+ time.AfterFunc(streamEndDrainGrace, cancel)}}()diff --git a/backend/api/ws/system_stats.go b/backend/api/ws/system_stats.goindex 127e43b0f..dd22a339f 100644--- a/backend/api/ws/system_stats.go+++ b/backend/api/ws/system_stats.go@@ -1,6 +1,7 @@package wsimport (+ "cmp""context""encoding/json/v2""log/slog"@@ -335,25 +336,26 @@ func (h *WebSocketHandler) readCPUUsage(interval time.Duration) (float64, bool)// @Param id path string true "Environment ID"// @Router /api/environments/{id}/ws/system/stats [get]func (h *WebSocketHandler) SystemStats(c *echo.Context) error {- clientIP := c.RealIP()+ userID, _ := c.Get("userID").(string)+ clientKey := cmp.Or(userID, c.RealIP())h.activeConnectionsMu.Lock()- if h.activeConnections[clientIP] >= 5 {+ if h.activeConnections[clientKey] >= 5 {h.activeConnectionsMu.Unlock()- return c.JSON(http.StatusTooManyRequests, map[string]any{"success": false, "error": "Too many concurrent stats connections from this IP"})+ return c.JSON(http.StatusTooManyRequests, map[string]any{"success": false, "error": "Too many concurrent stats connections for this client"})}if h.activeConnections == nil {h.activeConnections = make(map[string]int)}- h.activeConnections[clientIP]+++ h.activeConnections[clientKey]++h.activeConnectionsMu.Unlock()defer func() {h.activeConnectionsMu.Lock()defer h.activeConnectionsMu.Unlock()- if count := h.activeConnections[clientIP]; count > 1 {- h.activeConnections[clientIP] = count - 1+ if count := h.activeConnections[clientKey]; count > 1 {+ h.activeConnections[clientKey] = count - 1} else {- delete(h.activeConnections, clientIP)+ delete(h.activeConnections, clientKey)}}()@@ -364,15 +366,12 @@ func (h *WebSocketHandler) SystemStats(c *echo.Context) error {defer unregister()defer func() {if err := conn.CloseNow(); err != nil {- slog.DebugContext(c.Request().Context(), "Failed to close system stats websocket connection", "clientIp", clientIP, "error", err)+ slog.DebugContext(c.Request().Context(), "Failed to close system stats websocket connection", "clientKey", clientKey, "error", err)}}()interval, _ := httputil.GetIntQueryParam(c.Request(), "interval", false)- if interval <= 0 {- interval = 2- }- sampleInterval := time.Duration(interval) * time.Second+ sampleInterval := time.Duration(min(cmp.Or(max(interval, 0), 2), 60)) * time.Secondconn.SetReadLimit(512)diff --git a/backend/internal/activity/handler.go b/backend/internal/activity/handler.goindex 5704051a2..4e86a1765 100644--- a/backend/internal/activity/handler.go+++ b/backend/internal/activity/handler.go@@ -259,11 +259,11 @@ func (h *ActivityHandler) RunLocalStreamProducer(ctx context.Context, limit int,})}- snapshotOK := sendSnapshot()-eventCh, missedEvents, unsubscribe := h.activityService.pubsub.Subscribe("0")defer unsubscribe()+ snapshotOK := sendSnapshot()+ticker := time.NewTicker(activityStreamHeartbeatInterval)defer ticker.Stop()@@ -344,7 +344,6 @@ func activityStreamEnvironmentVersion(localEnvironment environment.Environment)func (h *ActivityHandler) runRemoteActivityStreamPoller(ctx context.Context, localEnvironment environment.Environment, limit int, publish func(activity.StreamEvent)) {environmentID := localEnvironment.ID- lastError := ""lastFingerprint := ""poll := func() {@@ -373,42 +372,30 @@ func (h *ActivityHandler) runRemoteActivityStreamPoller(ctx context.Context, locif err != nil && ctx.Err() != nil {return}- if err == nil && lastError != "" {- // Resend the snapshot once the environment recovers.- lastFingerprint = ""+ activities := kit.FromPtr(output).Body.Data+ sourceID := cmp.Or(currentEnvironment.ID, environment.LocalEnvironmentID)+ sourceName := environment.DisplayName(sourceID, currentEnvironment.Name)+ for i := range activities {+ applyActivitySource(&activities[i], sourceID, sourceName)}- if output != nil {- sourceID := cmp.Or(currentEnvironment.ID, environment.LocalEnvironmentID)- sourceName := environment.DisplayName(sourceID, currentEnvironment.Name)- for i := range output.Body.Data {- applyActivitySource(&output.Body.Data[i], sourceID, sourceName)- }- if fingerprint := strconv.FormatUint(kit.Fingerprint(output.Body.Data), 16); fingerprint != lastFingerprint {- publish(activity.StreamEvent{- Type: "snapshot",- EnvironmentID: environmentID,- Activities: output.Body.Data,- Timestamp: time.Now(),- })- lastFingerprint = fingerprint- lastError = ""+ errMessage := func() string {+ if err != nil {+ return err.Error()}- }- if err == nil {- lastError = ""+ return ""+ }()+ fingerprint := strconv.FormatUint(kit.Fingerprint(activities), 16) + "|" + errMessage+ if fingerprint == lastFingerprint {return}- // A failing environment must not end the stream; surface the error- // once per distinct message and keep polling.- if msg := err.Error(); msg != lastError {- lastError = msg- publish(activity.StreamEvent{- Type: "error",- EnvironmentID: environmentID,- Error: msg,- Timestamp: time.Now(),- })- }+ lastFingerprint = fingerprint+ publish(activity.StreamEvent{+ Type: kit.Ternary(err == nil, "snapshot", "error"),+ EnvironmentID: environmentID,+ Activities: activities,+ Error: errMessage,+ Timestamp: time.Now(),+ })}poll()diff --git a/backend/internal/activity/service.go b/backend/internal/activity/service.goindex df1cea574..78e97ddbb 100644--- a/backend/internal/activity/service.go+++ b/backend/internal/activity/service.go@@ -493,7 +493,11 @@ func (s *ActivityService) retryWrite(ctx context.Context, activityID, what strinreturn err}slog.WarnContext(ctx, "retrying activity write", "write", what, "activityId", activityID, "attempt", attempt, "error", err)- time.Sleep(250 * time.Millisecond * time.Duration(attempt))+ select {+ case <-ctx.Done():+ return errors.Join(err, context.Cause(ctx))+ case <-time.After(250 * time.Millisecond * time.Duration(attempt)):+ }}}diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex e91cb85e4..84e06b20d 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -30,6 +30,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/docker""github.com/getarcaneapp/arcane/backend/v2/internal/environment""github.com/getarcaneapp/arcane/backend/v2/internal/gitops"+ "github.com/getarcaneapp/arcane/backend/v2/internal/image""github.com/getarcaneapp/arcane/backend/v2/internal/project""github.com/getarcaneapp/arcane/backend/v2/internal/role""github.com/getarcaneapp/arcane/backend/v2/internal/settings"@@ -199,6 +200,7 @@ type initializeStartupStateParams struct {GitOpsSync *gitops.GitOpsSyncServiceProject *project.ProjectServiceContainer *container.ContainerService+ Image *image.ImageServiceVariable *variable.VariableServiceDocker *docker.DockerClientServiceSwarm *swarm.SwarmService@@ -260,6 +262,7 @@ func initializeStartupState(p initializeStartupStateParams) {{name: "recover interrupted project renames", run: p.Project.RecoverProjectRenameJournals},{name: "recover interrupted container redeploys", run: p.Container.RecoverInterruptedRedeploys},{name: "recover stuck project statuses", run: p.Project.RecoverStuckProjectStatuses},+ {name: "fail interrupted image patches", run: p.Image.RecoverInterruptedPatches},} {if err := task.run(appCtx); err != nil {slog.WarnContext(appCtx, "Startup task failed", "task", task.name, "error", err)diff --git a/backend/internal/common/errors.go b/backend/internal/common/errors.goindex 1046f85ef..5f25c7276 100644--- a/backend/internal/common/errors.go+++ b/backend/internal/common/errors.go@@ -1,6 +1,12 @@package common-import "errors"+import (+ "context"+ "errors"++ "github.com/containerd/errdefs"+ "github.com/moby/moby/client"+)const (// ConfigurationErrorCodeEnvFileUnreadable is the ConfigurationError code reported@@ -24,6 +30,23 @@ func Classify(kind, err error) error {return &classified{kind: kind, err: err}}+func ClassifyDockerError(err error) error {+ switch {+ case errdefs.IsNotFound(err):+ return Classify(ErrNotFound, err)+ case errdefs.IsConflict(err):+ return Classify(ErrConflict, err)+ case errdefs.IsInvalidArgument(err):+ return Classify(ErrBadRequest, err)+ case errdefs.IsUnavailable(err), client.IsErrConnectionFailed(err):+ return Classify(ErrUnavailable, err)+ case errors.Is(err, context.DeadlineExceeded):+ return Classify(ErrTimeout, err)+ default:+ return err+ }+}+func (e *classified) Error() string { return e.err.Error() }func (e *classified) Unwrap() error { return e.err }diff --git a/backend/internal/container/handler.go b/backend/internal/container/handler.goindex 6b46730df..654c9bd17 100644--- a/backend/internal/container/handler.go+++ b/backend/internal/container/handler.go@@ -7,7 +7,6 @@ import ("maps""strings"- "github.com/containerd/errdefs""github.com/danielgtaylor/huma/v2"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity""github.com/getarcaneapp/arcane/types/v2/base"@@ -340,7 +339,8 @@ func (h *ContainerHandler) CreateContainer(ctx context.Context, input *CreateConfunc (h *ContainerHandler) GetContainer(ctx context.Context, input *GetContainerInput) (*handlerutil.Out[container.Details], error) {details, err := h.containerService.GetContainerDetails(ctx, input.ContainerID)if err != nil {- return nil, huma.Error404NotFound("Failed to retrieve container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to retrieve container: "+apiErr.Message)}return &handlerutil.Out[container.Details]{@@ -369,10 +369,8 @@ func (h *ContainerHandler) GetContainerProcesses(ctx context.Context, input *Getfunc (h *ContainerHandler) DownloadContainerLogs(ctx context.Context, input *GetContainerInput) (*huma.StreamResponse, error) {reader, filename, err := h.containerService.DownloadLogs(ctx, input.ContainerID)if err != nil {- if errdefs.IsNotFound(err) {- return nil, huma.Error404NotFound("Failed to retrieve container: " + err.Error())- }- return nil, huma.Error500InternalServerError("Failed to download container logs: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to download container logs: "+apiErr.Message)}return handlerutil.DownloadResponse(reader, -1, filename), nil}@@ -402,7 +400,8 @@ func (h *ContainerHandler) StartContainer(ctx context.Context, input *ContainerAreturn h.containerService.StartContainer(runtimeCtx, containerID, user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to start container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to start container: "+apiErr.Message)},})}@@ -418,7 +417,8 @@ func (h *ContainerHandler) StopContainer(ctx context.Context, input *ContainerAcreturn h.containerService.StopContainer(runtimeCtx, containerID, user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to stop container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to stop container: "+apiErr.Message)},})}@@ -434,7 +434,8 @@ func (h *ContainerHandler) RestartContainer(ctx context.Context, input *Containereturn h.containerService.RestartContainer(runtimeCtx, containerID, user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to restart container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to restart container: "+apiErr.Message)},})}@@ -450,7 +451,8 @@ func (h *ContainerHandler) KillContainer(ctx context.Context, input *KillContainreturn h.containerService.KillContainer(runtimeCtx, containerID, strings.TrimSpace(input.Signal), user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to kill container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to kill container: "+apiErr.Message)},})}@@ -466,7 +468,8 @@ func (h *ContainerHandler) PauseContainer(ctx context.Context, input *ContainerAreturn h.containerService.PauseContainer(runtimeCtx, containerID, user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to pause container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to pause container: "+apiErr.Message)},})}@@ -482,7 +485,8 @@ func (h *ContainerHandler) UnpauseContainer(ctx context.Context, input *Containereturn h.containerService.UnpauseContainer(runtimeCtx, containerID, user)},Error: func(err error) error {- return huma.Error500InternalServerError("Failed to unpause container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return huma.NewError(apiErr.HTTPStatus(), "Failed to unpause container: "+apiErr.Message)},})}@@ -587,7 +591,8 @@ func (h *ContainerHandler) RedeployContainer(ctx context.Context, input *Containif err != nil {activitylib.FlushWriter(activityWriter)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container redeploy failed", err)- return nil, huma.Error500InternalServerError("Failed to redeploy container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to redeploy container: "+apiErr.Message)}activitylib.FlushWriter(activityWriter)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container redeployed", nil)@@ -639,7 +644,8 @@ func (h *ContainerHandler) RedeployContainer(ctx context.Context, input *Containfunc (h *ContainerHandler) GetContainerEditConfig(ctx context.Context, input *GetContainerEditConfigInput) (*handlerutil.Out[container.EditConfig], error) {editConfig, err := h.containerService.GetContainerEditConfig(ctx, input.ContainerID)if err != nil {- return nil, huma.Error404NotFound("Failed to retrieve container: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to retrieve container: "+apiErr.Message)}return &handlerutil.Out[container.EditConfig]{@@ -683,7 +689,7 @@ func (h *ContainerHandler) EditContainer(ctx context.Context, input *EditContainif err != nil {activitylib.FlushWriter(activityWriter)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container edit failed", err)- apiErr := common.ToAPIError(err)+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))return nil, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)}activitylib.FlushWriter(activityWriter)@@ -763,7 +769,8 @@ func (h *ContainerHandler) DeleteContainer(ctx context.Context, input *DeleteCon)if deleteContainerErr := h.containerService.DeleteContainer(runtimeCtx, input.ContainerID, input.Force, input.RemoveVolumes, *user); deleteContainerErr != nil {activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container deleted", deleteContainerErr)- return nil, huma.Error500InternalServerError("Failed to delete container: " + deleteContainerErr.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(deleteContainerErr))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to delete container: "+apiErr.Message)}activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Container deleted", nil)diff --git a/backend/internal/container/service.go b/backend/internal/container/service.goindex e0d7bd455..3442cf0cb 100644--- a/backend/internal/container/service.go+++ b/backend/internal/container/service.go@@ -11,6 +11,7 @@ import ("maps""net/netip""net/url"+ "path""slices""strings""sync"@@ -92,7 +93,6 @@ type containerLifecycleAction struct {action stringeventType event.EventTypemetadata database.JSON- warnOnLogError boolrunContainerAction func(context.Context) error}@@ -359,13 +359,12 @@ func (s *ContainerService) renameToRedeployBackup(ctx context.Context, containerreturn nil}-// stopContainer waits out a 30s grace period, so it runs without the Docker API deadline.func (s *ContainerService) stopContainer(ctx context.Context, containerID string) error {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return fmt.Errorf("failed to connect to Docker: %w", err)}- _, err = dockerClient.ContainerStop(ctx, containerID, client.ContainerStopOptions{Timeout: new(30)})+ _, err = dockerClient.ContainerStop(ctx, containerID, client.ContainerStopOptions{})return err}@@ -393,12 +392,8 @@ func (s *ContainerService) runContainerLifecycleAction(ctx context.Context, cont}maps.Copy(metadata, cfg.metadata)- err = s.eventService.LogContainerEvent(ctx, cfg.eventType, containerID, "name", user.ID, user.Username, "0", metadata)- if err != nil {- if !cfg.warnOnLogError {- return fmt.Errorf("failed to log action: %w", err)- }- slog.WarnContext(ctx, "could not log container action", "action", cfg.action, "error", err)+ if logErr := s.eventService.LogContainerEvent(ctx, cfg.eventType, containerID, "name", user.ID, user.Username, "0", metadata); logErr != nil {+ slog.WarnContext(ctx, "could not log container action", "action", cfg.action, "error", logErr)}defer s.eventService.BeginDockerResourceSuppressionWindow("container", containerID, "")()@@ -411,9 +406,8 @@ func (s *ContainerService) runContainerLifecycleAction(ctx context.Context, contfunc (s *ContainerService) StartContainer(ctx context.Context, containerID string, user usertypes.Actor) error {return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{- action: "start",- eventType: event.EventTypeContainerStart,- warnOnLogError: true,+ action: "start",+ eventType: event.EventTypeContainerStart,runContainerAction: func(ctx context.Context) error {_, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerStartResult, error) {return dockerClient.ContainerStart(ctx, containerID, client.ContainerStartOptions{})@@ -452,10 +446,9 @@ func (s *ContainerService) RestartContainer(ctx context.Context, containerID str// when signal is empty) without removing the container.func (s *ContainerService) KillContainer(ctx context.Context, containerID, signal string, user usertypes.Actor) error {return s.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{- action: "kill",- eventType: event.EventTypeContainerKill,- metadata: database.JSON{"signal": signal},- warnOnLogError: true,+ action: "kill",+ eventType: event.EventTypeContainerKill,+ metadata: database.JSON{"signal": signal},runContainerAction: func(ctx context.Context) error {_, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerKillResult, error) {return dockerClient.ContainerKill(ctx, containerID, client.ContainerKillOptions{Signal: signal})@@ -468,9 +461,8 @@ 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.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{- action: "pause",- eventType: event.EventTypeContainerPause,- warnOnLogError: true,+ action: "pause",+ eventType: event.EventTypeContainerPause,runContainerAction: func(ctx context.Context) error {_, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerPauseResult, error) {return dockerClient.ContainerPause(ctx, containerID, client.ContainerPauseOptions{})@@ -483,9 +475,8 @@ 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.runContainerLifecycleAction(ctx, containerID, user, containerLifecycleAction{- action: "unpause",- eventType: event.EventTypeContainerUnpause,- warnOnLogError: true,+ action: "unpause",+ eventType: event.EventTypeContainerUnpause,runContainerAction: func(ctx context.Context) error {_, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerUnpauseResult, error) {return dockerClient.ContainerUnpause(ctx, containerID, client.ContainerUnpauseOptions{})@@ -679,6 +670,23 @@ func (s *ContainerService) recreateContainer(// deferred until the replacement exists; only a start failure can still lose the original.stopAfterCreate := wasRunning && kit.FromPtr(containerInfo.HostConfig).AutoRemove+ var err error+ restoreHostConfig := kit.FromPtr(containerInfo.HostConfig)+ restoreHostConfig.Binds, restoreHostConfig.Mounts, err = docker.PreserveVolumeMounts(ctx, restoreHostConfig.Binds, restoreHostConfig.Mounts, containerInfo.Mounts)+ if err != nil {+ return "", fmt.Errorf("preserve volumes of container %s: %w", containerName, err)+ }+ createHostConfig := kit.FromPtr(newHostConfig)+ createHostConfig.Binds, createHostConfig.Mounts, err = docker.PreserveVolumeMounts(+ ctx,+ createHostConfig.Binds,+ createHostConfig.Mounts,+ retainedVolumeMountPoints(kit.FromPtr(containerInfo.HostConfig), createHostConfig, containerInfo.Mounts),+ )+ if err != nil {+ return "", fmt.Errorf("preserve volumes of container %s: %w", containerName, err)+ }+defer s.eventService.BeginDockerResourceSuppressionWindow("container", containerInfo.ID, containerName)()backupName := fmt.Sprintf("%s%s%d", cmp.Or(containerName, "arcane-redeploy-"+containerInfo.ID[:min(12, len(containerInfo.ID))]), redeployBackupInfix, time.Now().Unix())@@ -694,7 +702,7 @@ func (s *ContainerService) recreateContainer(restoreResp, restoreCreateErr := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerCreateResult, error) {return compat.ContainerCreateWithCompatibilityForAPIVersion(ctx, dockerClient, client.ContainerCreateOptions{Config: &originalConfig,- HostConfig: containerInfo.HostConfig,+ HostConfig: &restoreHostConfig,NetworkingConfig: buildCleanNetworkingConfig(containerInfo, apiVersion),Name: containerName,}, apiVersion)@@ -729,18 +737,18 @@ func (s *ContainerService) recreateContainer(s.eventService.MarkDockerExpectation("container", containerInfo.ID, containerName)if containerName != "" {- if err := s.renameToRedeployBackup(ctx, containerInfo.ID, containerName, backupName, newName, user); err != nil {- return "", err+ if renameErr := s.renameToRedeployBackup(ctx, containerInfo.ID, containerName, backupName, newName, user); renameErr != nil {+ return "", renameErr}}if wasRunning && !stopAfterCreate {- if err := s.stopContainer(ctx, containerInfo.ID); err != nil {+ if stopErr := s.stopContainer(ctx, containerInfo.ID); stopErr != nil {s.restoreContainerAfterRedeployFailure(ctx, containerInfo.ID, containerName, backupName, "stop", false, user)- s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerInfo.ID, containerName, user.ID, user.Username, "0", err, database.JSON{+ s.eventService.LogErrorEvent(ctx, event.EventTypeContainerError, "container", containerInfo.ID, containerName, user.ID, user.Username, "0", stopErr, database.JSON{"action": "redeploy","step": "stop",})- return "", fmt.Errorf("failed to stop container: %w", err)+ return "", fmt.Errorf("failed to stop container: %w", stopErr)}}@@ -752,7 +760,7 @@ func (s *ContainerService) recreateContainer(createResp, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerCreateResult, error) {return compat.ContainerCreateWithCompatibilityForAPIVersion(ctx, dockerClient, client.ContainerCreateOptions{Config: newConfig,- HostConfig: newHostConfig,+ HostConfig: &createHostConfig,NetworkingConfig: networkingConfig,Name: newName,}, apiVersion)@@ -788,8 +796,7 @@ func (s *ContainerService) recreateContainer(}}- restartPolicies := []container.RestartPolicyMode{container.RestartPolicyAlways, container.RestartPolicyUnlessStopped, container.RestartPolicyOnFailure}- if wasRunning || slices.Contains(restartPolicies, kit.FromPtr(containerInfo.HostConfig).RestartPolicy.Name) {+ if wasRunning {if _, err = s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerStartResult, error) {return dockerClient.ContainerStart(ctx, createResp.ID, client.ContainerStartOptions{})}); err != nil {@@ -881,6 +888,27 @@ func healthConfigFromCreate(hc *containertypes.HealthcheckCreate) *container.Hea}}+func retainedVolumeMountPoints(original, updated container.HostConfig, mountPoints []container.MountPoint) []container.MountPoint {+ specAt := func(hc container.HostConfig, destination string) (bool, bool) {+ if i := slices.IndexFunc(hc.Mounts, func(m mount.Mount) bool { return path.Clean(m.Target) == destination }); i >= 0 {+ return true, hc.Mounts[i].Type == mount.TypeVolume && hc.Mounts[i].Source == ""+ }+ for _, bind := range hc.Binds {+ parts := strings.SplitN(bind, ":", 3)+ if path.Clean(parts[min(1, len(parts)-1)]) == destination {+ return true, len(parts) == 1+ }+ }+ return false, false+ }++ return slices.DeleteFunc(slices.Clone(mountPoints), func(mp container.MountPoint) bool {+ inOriginal, _ := specAt(original, path.Clean(mp.Destination))+ inUpdated, anonymous := specAt(updated, path.Clean(mp.Destination))+ return kit.Ternary(inUpdated, !anonymous, inOriginal)+ })+}+// 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 {@@ -1250,18 +1278,7 @@ func (s *ContainerService) GetContainerProcesses(ctx context.Context, containerIreturn result, err})if err != nil {- 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 containertypes.Processes{}, common.ClassifyDockerError(fmt.Errorf("failed to list container processes: %w", err))}return containertypes.Processes{Titles: kit.Ternary(result.Titles == nil, []string{}, result.Titles),@@ -1353,12 +1370,11 @@ func (s *ContainerService) DeleteContainer(ctx context.Context, containerID stri}}- err = s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDelete, containerID, "name", user.ID, user.Username, "0", database.JSON{+ if logErr := s.eventService.LogContainerEvent(ctx, event.EventTypeContainerDelete, containerID, "name", user.ID, user.Username, "0", database.JSON{"action": "delete","containerId": containerID,- })- if err != nil {- return fmt.Errorf("failed to log action: %w", err)+ }); logErr != nil {+ slog.WarnContext(ctx, "could not log container action", "action", "delete", "error", logErr)}return nil@@ -2019,7 +2035,7 @@ func (s *ContainerService) CalculateStatusCounts(items []containertypes.Summary)}// CreateExec creates an exec instance in the container-func (s *ContainerService) CreateExec(ctx context.Context, containerID string, cmd []string) (string, error) {+func (s *ContainerService) CreateExec(ctx context.Context, containerID string, cmd []string, cols, rows uint) (string, error) {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return "", fmt.Errorf("failed to connect to Docker: %w", err)@@ -2030,6 +2046,7 @@ func (s *ContainerService) CreateExec(ctx context.Context, containerID string, cAttachStdout: true,AttachStderr: true,TTY: true,+ ConsoleSize: client.ConsoleSize{Height: rows, Width: cols},Cmd: cmd,})if err != nil {@@ -2039,6 +2056,16 @@ func (s *ContainerService) CreateExec(ctx context.Context, containerID string, creturn execResp.ID, nil}+func (s *ContainerService) ResizeExec(ctx context.Context, execID string, cols, rows uint) error {+ _, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ExecResizeResult, error) {+ return dockerClient.ExecResize(ctx, execID, client.ExecResizeOptions{Height: rows, Width: cols})+ })+ if err != nil {+ return fmt.Errorf("failed to resize exec %s: %w", execID, err)+ }+ return nil+}+func (e *ExecSession) Stdin() io.WriteCloser { return e.hijackedResp.Conn }func (e *ExecSession) Stdout() io.Reader { return e.hijackedResp.Reader }@@ -2049,13 +2076,32 @@ func (e *ExecSession) Close(ctx context.Context) error {e.closeOnce.Do(func() {slog.DebugContext(ctx, "Closing exec session", "execId", e.execID, "containerId", e.containerID)- // Send EOF (Ctrl-D) then exit to terminate the shell gracefully.- _, _ = e.hijackedResp.Conn.Write([]byte{0x04})- time.Sleep(50 * time.Millisecond)+ deadline, _ := ctx.Deadline()+ _ = e.hijackedResp.Conn.SetWriteDeadline(deadline)+ _, _ = e.hijackedResp.Conn.Write([]byte{0x03, 0x04})_, _ = e.hijackedResp.Conn.Write([]byte("exit\n"))- time.Sleep(100 * time.Millisecond)-e.hijackedResp.Close()++ pollCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)+ defer cancel()+ ticker := time.NewTicker(50 * time.Millisecond)+ defer ticker.Stop()+ for {+ inspect, err := e.dockerClient.ExecInspect(pollCtx, e.execID, client.ExecInspectOptions{})+ if err != nil {+ closeErr = fmt.Errorf("inspect exec %s: %w", e.execID, err)+ return+ }+ if !inspect.Running {+ return+ }+ select {+ case <-pollCtx.Done():+ closeErr = fmt.Errorf("exec %s still running with pid %d after session close", e.execID, inspect.PID)+ return+ case <-ticker.C:+ }+ }})return closeErrdiff --git a/backend/internal/container/service_test.go b/backend/internal/container/service_test.goindex 84f2f60ae..5dca416b1 100644--- a/backend/internal/container/service_test.go+++ b/backend/internal/container/service_test.go@@ -601,6 +601,60 @@ func TestBuildEditNetworkingConfig(t *testing.T) {require.Nil(t, out)}+func TestRetainedVolumeMountPoints(t *testing.T) {+ original := container.HostConfig{+ Binds: []string{"/cache", "removed:/removed", "kept:/kept"},+ Mounts: []mount.Mount{+ {Type: mount.TypeVolume, Target: "/anon-mount"},+ },+ }+ updated := container.HostConfig{+ Binds: []string{"/cache", "kept:/kept", "renamed:/image-volume"},+ Mounts: []mount.Mount{+ {Type: mount.TypeVolume, Target: "/anon-mount"},+ },+ }+ mountPoints := []container.MountPoint{+ {Type: mount.TypeVolume, Name: "anon-cache", Destination: "/cache"},+ {Type: mount.TypeVolume, Name: "removed", Destination: "/removed"},+ {Type: mount.TypeVolume, Name: "kept", Destination: "/kept"},+ {Type: mount.TypeVolume, Name: "anon-mount", Destination: "/anon-mount"},+ {Type: mount.TypeVolume, Name: "image-data", Destination: "/data"},+ {Type: mount.TypeVolume, Name: "image-other", Destination: "/image-volume"},+ }++ retained := retainedVolumeMountPoints(original, updated, mountPoints)++ require.Len(t, retained, 3)+ require.Equal(t, []string{"anon-cache", "anon-mount", "image-data"}, []string{retained[0].Name, retained[1].Name, retained[2].Name})+}++func TestContainerServiceResizeExec(t *testing.T) {+ db := setupProjectTestDB(t)+ var gotPath, gotHeight, gotWidth string+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ gotPath = r.Method + " " + dockerTestPath(r.URL.Path)+ gotHeight = r.URL.Query().Get("h")+ gotWidth = r.URL.Query().Get("w")+ w.WriteHeader(http.StatusOK)+ }))+ t.Cleanup(server.Close)++ svc := NewContainerService(+ event.NewEventService(db, nil, nil),+ docker.NewDockerClientService(t.Context(), nil, nil, nil).WithClient(newTestDockerClient(t, server)),+ nil,+ nil,+ nil,+ nil,+ )++ require.NoError(t, svc.ResizeExec(t.Context(), "exec-1", 120, 40))+ require.Equal(t, "POST /exec/exec-1/resize", gotPath)+ require.Equal(t, "40", gotHeight)+ require.Equal(t, "120", gotWidth)+}+func TestRecreateContainerDaemonCorrelation(t *testing.T) {db := setupProjectTestDB(t)events := event.NewEventService(db, nil, nil)diff --git a/backend/internal/diagnostics/service.go b/backend/internal/diagnostics/service.goindex 60eb43f66..5688123e7 100644--- a/backend/internal/diagnostics/service.go+++ b/backend/internal/diagnostics/service.go@@ -30,6 +30,7 @@ import (const (recentGCPauseSamples = 16goroutineLeakProfileName = "goroutineleak"+ leakScanCacheTTL = 30 * time.Second// maxActorDetailRowsInternal caps the instances and failures listed per actor type.maxActorDetailRowsInternal = 10@@ -45,8 +46,8 @@ type DiagnosticsService struct {coordinator *runs.Coordinatorenvironments *environment.EnvironmentService- leakMu sync.Mutex- leakScannedAt time.Time+ leakMu sync.Mutex+ leakReport system.GoroutineLeakReport}func NewDiagnosticsService(actors *francis.Runtime, coordinator *runs.Coordinator, environments *environment.EnvironmentService) *DiagnosticsService {@@ -61,7 +62,7 @@ func (s *DiagnosticsService) Collect() (system.RuntimeInfo, system.MemoryInfo, sdebug.ReadGCStats(&gc)s.leakMu.Lock()- leakScannedAt := s.leakScannedAt+ leakScannedAt := s.leakReport.ScannedAts.leakMu.Unlock()rt := system.RuntimeInfo{@@ -120,6 +121,12 @@ func (s *DiagnosticsService) Collect() (system.RuntimeInfo, system.MemoryInfo, s// ScanGoroutineLeaks runs a leak-detection GC and returns the leaked-goroutine// pprof text (debug=1). debug=2 would dump every goroutine, not only leaks.func (s *DiagnosticsService) ScanGoroutineLeaks() (system.GoroutineLeakReport, error) {+ s.leakMu.Lock()+ defer s.leakMu.Unlock()+ if time.Since(s.leakReport.ScannedAt) < leakScanCacheTTL {+ return s.leakReport, nil+ }+p := pprof.Lookup(goroutineLeakProfileName)if p == nil {return system.GoroutineLeakReport{}, errors.New("goroutineleak profile is not available")@@ -130,16 +137,12 @@ func (s *DiagnosticsService) ScanGoroutineLeaks() (system.GoroutineLeakReport, ereturn system.GoroutineLeakReport{}, fmt.Errorf("write goroutineleak profile: %w", err)}- now := time.Now().UTC()- s.leakMu.Lock()- s.leakScannedAt = now- s.leakMu.Unlock()-- return system.GoroutineLeakReport{+ s.leakReport = system.GoroutineLeakReport{Count: p.Count(),Profile: buf.String(),- ScannedAt: now,- }, nil+ ScannedAt: time.Now().UTC(),+ }+ return s.leakReport, nil}// Goroutines groups the live goroutines by identical stack, largest group first.diff --git a/backend/internal/diagnostics/service_test.go b/backend/internal/diagnostics/service_test.goindex 96e5e1337..d2c5d0319 100644--- a/backend/internal/diagnostics/service_test.go+++ b/backend/internal/diagnostics/service_test.go@@ -60,15 +60,25 @@ func TestDiagnosticsServiceScanGoroutineLeaks(t *testing.T) {assert.True(t, rt.LeakScannedAt.Equal(report.ScannedAt))}-func TestDiagnosticsServiceScanGoroutineLeaksDetectsLeak(t *testing.T) {+func TestDiagnosticsServiceScanGoroutineLeaksReturnsCachedReport(t *testing.T) {s := NewDiagnosticsService(nil, nil, nil)++ first, err := s.ScanGoroutineLeaks()+ require.NoError(t, err)+ second, err := s.ScanGoroutineLeaks()+ require.NoError(t, err)+ assert.True(t, second.ScannedAt.Equal(first.ScannedAt))+ assert.Equal(t, first.Profile, second.Profile)+}++func TestDiagnosticsServiceScanGoroutineLeaksDetectsLeak(t *testing.T) {leakUnbufferedChannelInternal()deadline := time.Now().Add(5 * time.Second)var reportCount intvar reportProfile stringfor {- report, err := s.ScanGoroutineLeaks()+ report, err := NewDiagnosticsService(nil, nil, nil).ScanGoroutineLeaks()require.NoError(t, err)reportCount = report.CountreportProfile = report.Profilediff --git a/backend/internal/docker/service.go b/backend/internal/docker/service.goindex e15ccb2d1..95730a52e 100644--- a/backend/internal/docker/service.go+++ b/backend/internal/docker/service.go@@ -198,7 +198,7 @@ func (s *DockerClientService) WatchEvents(ctx context.Context) {slog.WarnContext(ctx, "failed to connect to Docker event stream", "error", err)} else {result := dockerClient.Events(ctx, client.EventsListOptions{- Since: kit.Ternary(lastEventNano == 0, "", fmt.Sprintf("%d.%09d", lastEventNano/int64(time.Second), lastEventNano%int64(time.Second))),+ Since: kit.Ternary(lastEventNano == 0, "", fmt.Sprintf("%d.%09d", (lastEventNano+1)/int64(time.Second), (lastEventNano+1)%int64(time.Second))),})streamStart := time.Now()var streamErr errordiff --git a/backend/internal/docker/service_test.go b/backend/internal/docker/service_test.goindex fd803a156..267a0df9a 100644--- a/backend/internal/docker/service_test.go+++ b/backend/internal/docker/service_test.go@@ -322,7 +322,7 @@ func TestDockerClientService_WatchEventsResumesFromLastEventAfterRefresh(t *testselect {case got := <-reconnected:- assert.Equal(t, "1700000000.123456789", got.since)+ assert.Equal(t, "1700000000.123456790", got.since)// One probe from the initial GetClient, one from the RefreshClient before reconnecting.assert.GreaterOrEqual(t, got.pings, int32(2))case <-time.After(5 * time.Second):diff --git a/backend/internal/gitops/children/sync/service.go b/backend/internal/gitops/children/sync/service.goindex 8c3375199..49ac83eb4 100644--- a/backend/internal/gitops/children/sync/service.go+++ b/backend/internal/gitops/children/sync/service.go@@ -385,8 +385,8 @@ func (s *Service) syncComposeFile(ctx context.Context, sync *projectpkg.GitOpsSy// The non-suffixing create: a name collision means the binding is broken, never a "-N" duplicate.project, err := s.projectService.CreateProject(ctx, sync.ProjectName, source.composeContent, source.envContent, projecttypes.CreateProjectWorkspaceManifest{}, nil, nil, nil, actor, false)- if errors.Is(err, projects.ErrProjectDirExists) {- err = common.Classify(common.ErrGitOpsSyncProjectBindingBroken, fmt.Errorf("GitOps sync project binding broken: sync %s cannot create project %q: a directory with that name "++ if errors.Is(err, projects.ErrProjectDirExists) || errors.Is(err, common.ErrProjectWorkspaceConflict) {+ err = common.Classify(common.ErrGitOpsSyncProjectBindingBroken, fmt.Errorf("GitOps sync project binding broken: sync %s cannot create project %q: a project or directory with that name "+"already exists; refusing to create a duplicate", sync.ID, projects.SanitizeProjectName(sync.ProjectName)))return nil, false, s.failSync(ctx, result, sync, actor, "GitOps project binding broken", err)}diff --git a/backend/internal/image/children/patch/service.go b/backend/internal/image/children/patch/service.goindex 57e1d57e6..b1952ccbc 100644--- a/backend/internal/image/children/patch/service.go+++ b/backend/internal/image/children/patch/service.go@@ -575,7 +575,9 @@ func (s *Service) finishPatchRecord(ctx context.Context, record *ImagePatchRecorif packagesUpdated != nil {updates["packages_updated"] = *packagesUpdated}- if err := s.db.WithContext(utils.ActivityRuntimeContext(ctx, nil)).+ writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)+ defer cancel()+ if err := s.db.WithContext(writeCtx).Model(&ImagePatchRecord{}).Where("id = ?", record.ID).Updates(updates).Error; err != nil {@@ -583,6 +585,19 @@ func (s *Service) finishPatchRecord(ctx context.Context, record *ImagePatchRecor}}+func (s *Service) RecoverInterruptedPatches(ctx context.Context) error {+ if err := s.db.WithContext(ctx).+ Model(&ImagePatchRecord{}).+ Where("status = ?", string(imagepatch.PatchStatusPatching)).+ Updates(map[string]any{+ "status": string(imagepatch.PatchStatusFailed),+ "error": errPatchInterrupted.Error(),+ }).Error; err != nil {+ return fmt.Errorf("failed to fail interrupted image patches: %w", err)+ }+ return nil+}+// ListPatches returns the paginated patch history for an environment.func (s *Service) ListPatches(ctx context.Context, envID string, params pagination.QueryParams) ([]imagepatch.PatchRecord, pagination.Response, error) {var records []ImagePatchRecord@@ -923,7 +938,9 @@ func (s *Service) completePatchActivity(ctx context.Context, activityID string,}}()- if _, err := s.activityService.CompleteActivity(utils.ActivityRuntimeContext(ctx, nil), activityID, status, message, errorPtr); err != nil {+ writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)+ defer cancel()+ if _, err := s.activityService.CompleteActivity(writeCtx, activityID, status, message, errorPtr); err != nil {slog.DebugContext(ctx, "failed to complete image patch activity", "activityId", activityID, "error", err)}}diff --git a/backend/internal/image/children/patch/service_test.go b/backend/internal/image/children/patch/service_test.goindex dd179daf4..e1c137578 100644--- a/backend/internal/image/children/patch/service_test.go+++ b/backend/internal/image/children/patch/service_test.go@@ -21,6 +21,7 @@ import ("github.com/moby/moby/client""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ "go.getarcane.app/kit/pkg""gorm.io/gorm""github.com/getarcaneapp/arcane/backend/v2/internal/common"@@ -308,3 +309,30 @@ func TestAggregatePatchTargetsMergesEnvironments(t *testing.T) {assert.Equal(t, []string{"b/alpine:3", "a/nginx:1"}, rowLabels(merge("fixableCount", pagination.SortDesc, 0, 2).Data))assert.Equal(t, []string{"a/redis:7", "a/nginx:1", "b/alpine:3"}, rowLabels(merge("environment", pagination.SortAsc, 0, -1).Data))}++func TestRecoverInterruptedPatchesFailsLeftoverPatchingRecords(t *testing.T) {+ _, db := patchFeatureSettings(t)+ svc := &Service{db: db}+ for _, record := range []ImagePatchRecord{+ {+ BaseModel: database.BaseModel{ID: "stuck"},+ Status: string(imagepatch.PatchStatusPatching),+ },+ {+ BaseModel: database.BaseModel{ID: "done"},+ Status: string(imagepatch.PatchStatusCompleted),+ },+ } {+ require.NoError(t, db.Create(&record).Error)+ }++ require.NoError(t, svc.RecoverInterruptedPatches(t.Context()))++ var stuck, done ImagePatchRecord+ require.NoError(t, db.First(&stuck, "id = ?", "stuck").Error)+ require.NoError(t, db.First(&done, "id = ?", "done").Error)+ assert.Equal(t, string(imagepatch.PatchStatusFailed), stuck.Status)+ assert.Equal(t, errPatchInterrupted.Error(), kit.FromPtr(stuck.Error))+ assert.Equal(t, string(imagepatch.PatchStatusCompleted), done.Status)+ assert.Nil(t, done.Error)+}diff --git a/backend/internal/image/handler.go b/backend/internal/image/handler.goindex 1ecd1ef1c..629539934 100644--- a/backend/internal/image/handler.go+++ b/backend/internal/image/handler.go@@ -351,10 +351,7 @@ func (h *ImageHandler) PullImage(ctx context.Context, input *PullImageInput) (*hif pullImageErr := h.imageService.PullImage(runtimeCtx, fullImageName, writer, *user, credentials); pullImageErr != nil {activitylib.FlushWriter(writer)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Image pull failed", pullImageErr)- _, _ = fmt.Fprintf(writer, "{\"error\":%q}\n", pullImageErr.Error())- if f, ok := writer.(http.Flusher); ok {- f.Flush()- }+ activitylib.WriteErrorLine(rawWriter, pullImageErr)return}activitylib.FlushWriter(writer)@@ -405,10 +402,7 @@ func (h *ImageHandler) BuildImage(ctx context.Context, input *BuildImageInput) (if _, buildImageErr := h.buildService.BuildImage(runtimeCtx, input.EnvironmentID, input.Body, writer, "", user); buildImageErr != nil {activitylib.FlushWriter(writer)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, "Image build failed", buildImageErr)- _, _ = fmt.Fprintf(writer, "{\"error\":%q}\n", buildImageErr.Error())- if f, ok := writer.(http.Flusher); ok {- f.Flush()- }+ activitylib.WriteErrorLine(rawWriter, buildImageErr)return}activitylib.FlushWriter(writer)diff --git a/backend/internal/image/service.go b/backend/internal/image/service.goindex a74c79679..f171ec945 100644--- a/backend/internal/image/service.go+++ b/backend/internal/image/service.go@@ -219,6 +219,10 @@ func (s *ImageService) RegisterWorkflows(engine *flow.Engine) error {return nil}+func (s *ImageService) RecoverInterruptedPatches(ctx context.Context) error {+ return s.patch.RecoverInterruptedPatches(ctx)+}+// PatchWorkflow is the scheduled auto-patch workflow.func (s *ImageService) PatchWorkflow() *flow.Workflow { return s.patchWorkflow }diff --git a/backend/internal/imageupdate/children/tags/service.go b/backend/internal/imageupdate/children/tags/service.goindex aa2d6e149..a81443615 100644--- a/backend/internal/imageupdate/children/tags/service.go+++ b/backend/internal/imageupdate/children/tags/service.go@@ -4,6 +4,7 @@ import ("cmp""context""errors"+ "fmt""log/slog""maps""slices"@@ -14,6 +15,7 @@ import ("github.com/getarcaneapp/arcane/types/v2/imageupdate""github.com/moby/moby/api/types/container""github.com/moby/moby/client"+ kit "go.getarcane.app/kit/pkg""go.getarcane.app/updater""go.getarcane.app/updater/pkg/utils/tagpolicy""go.getarcane.app/updater/refs"@@ -58,12 +60,12 @@ type tagRegistry struct {// scanRegistryMemo memoizes successful registry lookups for one scan// and coalesces concurrent misses for the same key into a single request.-// Failures are not stored: each waiting caller receives the shared error and-// the next caller retries, so a transient fault never sticks for the scan.type scanRegistryMemo[V any] struct {- flight singleflight.Group- mu sync.Mutex- values map[string]V+ flight singleflight.Group+ mu sync.Mutex+ values map[string]V+ failures map[string]error+ keepFailure func(error) bool}func NewService(@@ -99,8 +101,16 @@ func (s *Service) Check(credentials: credentials,docker: s.docker,settings: s.settings,- tags: &scanRegistryMemo[[]string]{values: map[string][]string{}},- digests: &scanRegistryMemo[string]{values: map[string]string{}},+ tags: &scanRegistryMemo[[]string]{+ values: map[string][]string{},+ failures: map[string]error{},+ keepFailure: func(err error) bool { return registry.IsRateLimitErrorString(err.Error()) },+ },+ digests: &scanRegistryMemo[string]{+ values: map[string]string{},+ failures: map[string]error{},+ keepFailure: func(error) bool { return false },+ },}engine, err := updater.New(updater.Config{RegistryTagLister: adapter,@@ -150,7 +160,15 @@ func (s *Service) Check(checks[i] = s.checkContainerTag(groupCtx, engine, adapter, cnt)saveMu.Lock()defer saveMu.Unlock()- return save(groupCtx, cnt, checks[i])+ saveErr := save(groupCtx, cnt, checks[i])+ if saveErr == nil || groupCtx.Err() != nil {+ return saveErr+ }+ checks[i].Error = errors.Join(+ kit.Ternary(checks[i].Error != "", errors.New(checks[i].Error), nil),+ fmt.Errorf("save tag check result: %w", saveErr),+ ).Error()+ return nil})}waitErr := g.Wait()@@ -166,13 +184,23 @@ func (s *Service) Check(func (m *scanRegistryMemo[V]) do(key string, fetch func() (V, error)) (V, error) {m.mu.Lock()value, ok := m.values[key]+ failure := m.failures[key]m.mu.Unlock()if ok {return value, nil}+ if failure != nil {+ var zero V+ return zero, failure+ }shared, err, _ := m.flight.Do(key, func() (any, error) {fetched, fetchErr := fetch()if fetchErr != nil {+ if m.keepFailure(fetchErr) {+ m.mu.Lock()+ m.failures[key] = fetchErr+ m.mu.Unlock()+ }return nil, fetchErr}m.mu.Lock()diff --git a/backend/internal/imageupdate/service.go b/backend/internal/imageupdate/service.goindex 07aadb249..66db06df9 100644--- a/backend/internal/imageupdate/service.go+++ b/backend/internal/imageupdate/service.go@@ -47,9 +47,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/timeouts""github.com/getarcaneapp/arcane/backend/v2/pkg/projects""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/flow"- "github.com/getarcaneapp/arcane/backend/v2/pkg/utils""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref"- "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/notifications")const (@@ -246,7 +244,9 @@ func (s *ImageUpdateService) completeImageUpdateActivity(ctx context.Context, acreturn activitytypes.StatusFailed, mo.EmptyableToOption(strings.TrimSpace(message)).ToPointer(), cmp.Or(message, "Image update check completed")}}()- if _, err := s.activityService.CompleteActivity(utils.ActivityRuntimeContext(ctx, nil), activityID, status, finalMessage, errMessage, "Image update check complete"); err != nil {+ writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)+ defer cancel()+ if _, err := s.activityService.CompleteActivity(writeCtx, activityID, status, finalMessage, errMessage, "Image update check complete"); err != nil {// A lost terminal write strands the activity in running forever, so it// must be loud enough to correlate with a stuck activity panel entry.slog.ErrorContext(ctx, "failed to complete image update activity", "activityId", activityID, "error", err)@@ -318,6 +318,11 @@ func (s *ImageUpdateService) CheckImageUpdate(ctx context.Context, imageRef stri}()containerUpdates, tagErr := s.checkContainerTagUpdates(ctx, kit.Ternary(digestResult.UpdateType == UpdateTypeLocal, nil, []string{imageRef}), nil)if tagErr != nil {+ if err == nil {+ if saveErr := s.saveUpdateResultWithSnapshot(ctx, imageRef, digestResult, snapshot); saveErr != nil {+ slog.WarnContext(ctx, "Failed to save update result", "imageRef", imageRef, "error", saveErr.Error())+ }+ }s.completeImageUpdateActivity(ctx, activityID, false, tagErr.Error())return digestResult, tagErr}@@ -367,40 +372,8 @@ func (s *ImageUpdateService) CheckImageUpdate(ctx context.Context, imageRef strislog.WarnContext(ctx, "Failed to save update result", "imageRef", imageRef, "error", saveErr.Error())}- // Mark the record notified once at least one provider delivered: re-sending a partial- // failure on every poll would duplicate it forever; only delivered == 0 is retried.- if digestResult.HasUpdate {- delivered, notifErr := s.notificationService.SendImageUpdateNotification(ctx, imageRef, digestResult, notifications.NotificationEventImageUpdate)- if notifErr != nil {- slog.WarnContext(ctx, "Failed to send update notification", "imageRef", imageRef, "error", notifErr.Error())- }- if delivered > 0 {- // Prefer the snapshot's image ID: it is the same key saveUpdateResultWithSnapshot stored the record under.- imageID, imageIDErr := func() (string, error) {- if kit.FromPtr(snapshot).ImageID != "" {- return snapshot.ImageID, nil- }- inspectResponse, inspectErr := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ImageInspectResult, error) {- return dockerClient.ImageInspect(ctx, imageRef)- })- if inspectErr != nil {- return "", fmt.Errorf("image not found: %w", inspectErr)- }- return inspectResponse.ID, nil- }()- switch {- case imageIDErr != nil:- slog.WarnContext(ctx, "Failed to resolve image ID to mark notified", "imageRef", imageRef, "error", imageIDErr.Error())- case imageID != "":- if markErr := s.MarkUpdatesAsNotified(ctx, []string{imageID}); markErr != nil {- slog.WarnContext(ctx, "Failed to mark update as notified", "imageRef", imageRef, "error", markErr.Error())- }- }- }- }-result := tags.AttachContainerUpdates(map[string]*imageupdate.Response{imageRef: digestResult}, containerUpdates)[imageRef]- if len(containerUpdates) > 0 {+ if digestResult.HasUpdate || len(containerUpdates) > 0 {// A failed flush is already logged; the records stay pending for the next one._ = s.SendBatchUpdateNotifications(ctx)}@@ -823,7 +796,7 @@ func buildImageUpdateRecord(imageID, repo, tag string, result *imageupdate.RespoUpdateType: result.UpdateType,CurrentVersion: cmp.Or(result.CurrentVersion, tag),ResponseTimeMs: result.ResponseTimeMs,- HasUpdate: result.HasUpdate,+ HasUpdate: result.HasUpdate || kit.FromPtr(carried).HasUpdate,UsedCredential: result.UsedCredential,NotificationSent: notificationSent,}@@ -854,10 +827,10 @@ func savePreparedUpdateResultWithTx(tx *gorm.DB, imageID, repo, tag string, resu// value if nothing changed. New records start with notification_sent = false.// latest_seen_at keeps the time the pending update was first seen until it changes.return tx.Save(buildImageUpdateRecord(imageID, repo, tag, result,- hasExisting &&- existingRecord.HasUpdate == result.HasUpdate &&- (!result.HasUpdate || sameLatest) &&- existingRecord.NotificationSent,+ existingRecord.NotificationSent &&+ (failed || hasExisting &&+ existingRecord.HasUpdate == result.HasUpdate &&+ (!result.HasUpdate || sameLatest)),func() *time.Time {switch {case failed:@@ -1016,16 +989,26 @@ func (s *ImageUpdateService) GetUnnotifiedUpdates(ctx context.Context) (map[strireturn result, nil}-// MarkUpdatesAsNotified marks the given image IDs as having been notified-func (s *ImageUpdateService) MarkUpdatesAsNotified(ctx context.Context, imageIDs []string) error {- if len(imageIDs) == 0 {+func (s *ImageUpdateService) MarkUpdatesAsNotified(ctx context.Context, records []*ImageUpdateRecord) error {+ if len(records) == 0 {return nil}- return s.db.WithContext(ctx).- Model(&ImageUpdateRecord{}).- Where("id IN ?", imageIDs).- Update("notification_sent", true).Error+ return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {+ for _, record := range records {+ if err := tx.Model(&ImageUpdateRecord{}).+ Where(map[string]any{+ "id": record.ID,+ "check_time": record.CheckTime,+ "latest_digest": record.LatestDigest,+ "latest_version": record.LatestVersion,+ }).+ Update("notification_sent", true).Error; err != nil {+ return err+ }+ }+ return nil+ })}// groupImageRefs dedupes refs naming the same registry/repo:tag into one@@ -1632,9 +1615,8 @@ func (s *ImageUpdateService) SendBatchUpdateNotifications(ctx context.Context) ereturn fmt.Errorf("flush pending update notifications: %w", err)case len(unnotifiedUpdates) > 0:updatesToNotify := make(map[string]*imageupdate.Response)- imageIDsToMark := make([]string, 0, len(unnotifiedUpdates))- for imageID, record := range unnotifiedUpdates {+ for _, record := range unnotifiedUpdates {updatesToNotify[record.Repository+":"+record.Tag+kit.Ternary(record.ContainerID != "", " (container "+record.ContainerID+")", "")] = &imageupdate.Response{HasUpdate: record.HasUpdate,UpdateType: record.UpdateType,@@ -1650,7 +1632,6 @@ func (s *ImageUpdateService) SendBatchUpdateNotifications(ctx context.Context) eAuthRegistry: kit.FromPtr(record.AuthRegistry),UsedCredential: record.UsedCredential,}- imageIDsToMark = append(imageIDsToMark, imageID)}slog.InfoContext(ctx, "Sending notifications for unnotified updates", "count", len(updatesToNotify))@@ -1663,10 +1644,10 @@ func (s *ImageUpdateService) SendBatchUpdateNotifications(ctx context.Context) e// failure must not make every healthy provider re-send the same updates on// the next poll. Failures remain visible in the delivery history.if delivered == 0 {- slog.DebugContext(ctx, "No providers delivered image update notifications; leaving records unnotified", "count", len(imageIDsToMark))+ slog.DebugContext(ctx, "No providers delivered image update notifications; leaving records unnotified", "count", len(unnotifiedUpdates))return nil}- if markErr := s.MarkUpdatesAsNotified(notifCtx, imageIDsToMark); markErr != nil {+ if markErr := s.MarkUpdatesAsNotified(notifCtx, slices.Collect(maps.Values(unnotifiedUpdates))); markErr != nil {slog.WarnContext(ctx, "Failed to mark updates as notified", "error", markErr.Error())}default:@@ -1697,7 +1678,7 @@ func (s *ImageUpdateService) CleanupOrphanedRecords(ctx context.Context) error {dockerImageIDs = append(dockerImageIDs, img.ID)}- imageScoped := s.db.WithContext(ctx).Where("container_id = ? AND project_id = ?", "", "")+ imageScoped := s.db.WithContext(ctx).Where("container_id = ? AND project_id = ? AND id NOT LIKE 'ref::%'", "", "")if len(dockerImageIDs) > 0 {imageScoped = imageScoped.Where("id NOT IN ?", dockerImageIDs)}diff --git a/backend/internal/imageupdate/service_test.go b/backend/internal/imageupdate/service_test.goindex 7894e8f60..4fab8f430 100644--- a/backend/internal/imageupdate/service_test.go+++ b/backend/internal/imageupdate/service_test.go@@ -1811,8 +1811,11 @@ func TestImageUpdateService_MarkUpdatesAsNotified(t *testing.T) {require.NoError(t, err)}+ var pending []*ImageUpdateRecord+ require.NoError(t, db.Where("id IN ?", []string{"sha256:img1", "sha256:img2"}).Find(&pending).Error)+// Mark img1 and img2 as notified- err := svc.MarkUpdatesAsNotified(ctx, []string{"sha256:img1", "sha256:img2"})+ err := svc.MarkUpdatesAsNotified(ctx, pending)require.NoError(t, err)// Verify img1 and img2 are marked@@ -1840,13 +1843,36 @@ func TestImageUpdateService_MarkUpdatesAsNotified_EmptyList(t *testing.T) {svc := &ImageUpdateService{db: db}// Should not error on empty list- err := svc.MarkUpdatesAsNotified(ctx, []string{})+ err := svc.MarkUpdatesAsNotified(ctx, []*ImageUpdateRecord{})require.NoError(t, err)err = svc.MarkUpdatesAsNotified(ctx, nil)require.NoError(t, err)}+func TestImageUpdateService_CleanupOrphanedRecordsKeepsRefRecords(t *testing.T) {+ db := setupImageUpdateTestDB(t)+ server := newImageUpdateDiscoveryServer(t, []image.Summary{{ID: "sha256:present"}}, nil)+ t.Cleanup(server.Close)+ svc := &ImageUpdateService{+ db: db,+ dockerService: &docker.DockerClientService{Client: newImageUpdateTestDockerClient(t, server)},+ }+ for _, id := range []string{"sha256:present", "sha256:gone", "ref::docker.io/library/nginx@latest"} {+ require.NoError(t, db.Create(&ImageUpdateRecord{+ ID: id,+ Repository: "test/repo",+ Tag: "latest",+ }).Error)+ }++ require.NoError(t, svc.CleanupOrphanedRecords(t.Context()))++ var ids []string+ require.NoError(t, db.Model(&ImageUpdateRecord{}).Pluck("id", &ids).Error)+ assert.ElementsMatch(t, []string{"sha256:present", "ref::docker.io/library/nginx@latest"}, ids)+}+// TestImageUpdateService_SendBatchNotifications_DetachesCanceledContext guards the// regression where completeImageUpdateActivity cancels the activity-tracked// ctx before the batch notification step runs, killing the unnotified-updates querydiff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.goindex e11a762b7..c07c4bbf9 100644--- a/backend/internal/network/handler.go+++ b/backend/internal/network/handler.go@@ -191,7 +191,8 @@ func (h *NetworkHandler) CreateNetwork(ctx context.Context, input *CreateNetworkfunc (h *NetworkHandler) GetNetwork(ctx context.Context, input *GetNetworkInput) (*handlerutil.Out[networktypes.Inspect], error) {networkInspect, err := h.networkService.GetNetworkByID(ctx, input.NetworkID)if err != nil {- return nil, huma.Error404NotFound("Network not found: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to retrieve network: "+apiErr.Message)}out, err := mapping.MapOne[network.Inspect, networktypes.Inspect](*networkInspect)@@ -237,7 +238,8 @@ func (h *NetworkHandler) GetNetwork(ctx context.Context, input *GetNetworkInput)func (h *NetworkHandler) GetNetworkTopology(ctx context.Context, input *GetNetworkTopologyInput) (*handlerutil.Out[networktypes.Topology], error) {topology, err := h.networkService.GetNetworkTopology(ctx)if err != nil {- return nil, huma.Error500InternalServerError("failed to build network topology")+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to build network topology: "+apiErr.Message)}return &handlerutil.Out[networktypes.Topology]{@@ -272,7 +274,8 @@ func (h *NetworkHandler) DeleteNetwork(ctx context.Context, input *DeleteNetworkreturn h.networkService.RemoveNetwork(runtimeCtx, input.NetworkID, *user)})if err != nil {- return nil, huma.Error500InternalServerError("Failed to remove network: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to remove network: "+apiErr.Message)}return handlerutil.MessageOutput("Network removed successfully", activityID), nil@@ -303,7 +306,7 @@ func (h *NetworkHandler) ConnectContainer(ctx context.Context, input *ConnectConreturn h.networkService.ConnectContainer(runtimeCtx, input.NetworkID, input.Body, *user)})if err != nil {- apiErr := common.ToAPIError(err)+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))return nil, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)}@@ -336,7 +339,8 @@ func (h *NetworkHandler) DisconnectContainer(ctx context.Context, input *Disconnreturn h.networkService.DisconnectContainer(runtimeCtx, input.NetworkID, input.Body, *user)})if err != nil {- return nil, huma.Error500InternalServerError("Failed to disconnect container from network: " + err.Error())+ apiErr := common.ToAPIError(common.ClassifyDockerError(err))+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to disconnect container from network: "+apiErr.Message)}return handlerutil.MessageOutput("Container disconnected successfully", activityID), nildiff --git a/backend/internal/network/service.go b/backend/internal/network/service.goindex b3c581d8e..02b2552df 100644--- a/backend/internal/network/service.go+++ b/backend/internal/network/service.go@@ -14,6 +14,7 @@ import ("sync/atomic""time"+ "github.com/containerd/errdefs""github.com/getarcaneapp/arcane/types/v2/base"networktypes "github.com/getarcaneapp/arcane/types/v2/network"usertypes "github.com/getarcaneapp/arcane/types/v2/user"@@ -148,6 +149,9 @@ func (s *NetworkService) buildTopology(ctx context.Context) (*networktypes.Topolinspected, inspectErr := s.dockerService.Call(groupCtx, func(ctx context.Context, dockerClient *client.Client) (client.NetworkInspectResult, error) {return compat.NetworkInspectWithCompatibility(ctx, dockerClient, rawNetwork.ID, client.NetworkInspectOptions{})})+ if errdefs.IsNotFound(inspectErr) {+ return nil+ }if inspectErr != nil {return fmt.Errorf("failed to inspect network %s: %w", rawNetwork.Name, inspectErr)}@@ -165,6 +169,9 @@ func (s *NetworkService) buildTopology(ctx context.Context) (*networktypes.Topoledges := make([]networktypes.TopologyEdge, 0)containerNodeIDs := make(map[string]struct{})for i, rawNetwork := range networkList.Items {+ if inspectedNetworks[i].ID == "" {+ continue+ }nodes = append(nodes, networktypes.TopologyNode{ID: rawNetwork.ID,Name: rawNetwork.Name,diff --git a/backend/internal/port/handler.go b/backend/internal/port/handler.goindex 400e868a4..25d272496 100644--- a/backend/internal/port/handler.go+++ b/backend/internal/port/handler.go@@ -30,14 +30,14 @@ func (h *PortHandler) ListPorts(ctx context.Context, input *ListPortsInput) (*haif _, ok := httpx.EnvironmentScopeFromContext(ctx); ok {body, err := h.portService.AggregatePorts(ctx, params)if err != nil {- return nil, huma.Error500InternalServerError("failed to list ports")+ return nil, huma.Error500InternalServerError("failed to list ports: " + err.Error())}return &handlerutil.Page[port.PortMapping]{Body: body}, nil}items, paginationResp, err := h.portService.ListPortsPaginated(ctx, params)if err != nil {- return nil, huma.Error500InternalServerError("failed to list ports")+ return nil, huma.Error500InternalServerError("failed to list ports: " + err.Error())}return &handlerutil.Page[port.PortMapping]{diff --git a/backend/internal/port/service.go b/backend/internal/port/service.goindex a3400a3fe..6ec7ac3e1 100644--- a/backend/internal/port/service.go+++ b/backend/internal/port/service.go@@ -204,9 +204,6 @@ func compareOptionalIntDescInternal(a, b int, fallbackA, fallbackB string) int {}func compareOptionalStringInternal(a, b, fallbackA, fallbackB string) int {- a = normalizeOptionalStringSortValueInternal(a)- b = normalizeOptionalStringSortValueInternal(b)-switch {case a == "" && b == "":return strings.Compare(fallbackA, fallbackB)@@ -223,9 +220,6 @@ func compareOptionalStringInternal(a, b, fallbackA, fallbackB string) int {}func compareOptionalStringDescInternal(a, b, fallbackA, fallbackB string) int {- a = normalizeOptionalStringSortValueInternal(a)- b = normalizeOptionalStringSortValueInternal(b)-switch {case a == "" && b == "":return strings.Compare(fallbackA, fallbackB)@@ -240,7 +234,3 @@ func compareOptionalStringDescInternal(a, b, fallbackA, fallbackB string) int {return strings.Compare(fallbackA, fallbackB)}}--func normalizeOptionalStringSortValueInternal(value string) string {- return kit.Ternary(strings.EqualFold(strings.TrimSpace(value), "invalid IP"), "", value)-}diff --git a/backend/internal/port/service_test.go b/backend/internal/port/service_test.goindex c0d9c7d29..f58c59a8a 100644--- a/backend/internal/port/service_test.go+++ b/backend/internal/port/service_test.go@@ -220,7 +220,7 @@ func TestPortService_ListPortsPaginated_SortsByHostIPDescWithUnpublishedLast(t *assert.Equal(t, []string{"127.0.0.1", "0.0.0.0", ""}, []string{items[0].HostIP,items[1].HostIP,- normalizeOptionalStringSortValueInternal(items[2].HostIP),+ items[2].HostIP,})}diff --git a/backend/internal/project/children/details/service.go b/backend/internal/project/children/details/service.goindex 5c0cac4c6..bf79d2a58 100644--- a/backend/internal/project/children/details/service.go+++ b/backend/internal/project/children/details/service.go@@ -62,34 +62,32 @@ func (s *Service) ComposeContainers(ctx context.Context) ([]container.Summary, ereturn projects.ListGlobalComposeContainers(ctx, cli, s.dockerService.DockerHost())}+func RuntimeProjectName(fallback string, runtimeContainers []container.Summary) string {+ if len(runtimeContainers) == 0 {+ return fallback+ }+ return cmp.Or(docker.ComposeProjectLabel(runtimeContainers[0].Labels), fallback)+}+// ComposeServices reports every Compose service with its container state,// including services that have no container yet.func (s *Service) ComposeServices(ctx context.Context,composeProject *types.Project,- composeFileFullPath, projectsDirectory string,- autoInjectEnv bool,+ runtimeContainers []container.Summary,+ meta projects.ArcaneComposeMetadata,catalog string,) ([]project.RuntimeService, error) {- meta, metaErr := projects.ParseArcaneComposeMetadata(ctx, composeFileFullPath, projectsDirectory, autoInjectEnv)- if metaErr != nil {- slog.WarnContext(ctx, "failed to parse Arcane compose metadata", "path", composeFileFullPath, "error", metaErr)- }-- containers, err := projects.ComposePs(ctx, s.dockerService.DockerHost(), composeProject, nil, true)+ runtimeName := RuntimeProjectName(composeProject.Name, runtimeContainers)+ containers, err := projects.ComposePs(ctx, s.dockerService.DockerHost(), &types.Project{Name: runtimeName}, nil, true)if err != nil {- slog.ErrorContext(ctx, "compose ps error", "projectName", composeProject.Name, "error", err)+ slog.ErrorContext(ctx, "compose ps error", "projectName", runtimeName, "error", err)return nil, fmt.Errorf("failed to get compose services status: %w", err)}- // Compose ps omits image IDs; a listing failure only leaves image-level checks unmatched.- imageIDs := make(map[string]string)- if summaries, listErr := s.ComposeContainers(ctx); listErr != nil {- slog.WarnContext(ctx, "failed to list containers for project image identity", "projectName", composeProject.Name, "error", listErr)- } else {- for _, c := range summaries {- if docker.ComposeProjectLabel(c.Labels) == composeProject.Name && c.ImageID != "" {- imageIDs[c.ID] = c.ImageID- }+ imageIDs := make(map[string]string, len(runtimeContainers))+ for _, c := range runtimeContainers {+ if c.ImageID != "" {+ imageIDs[c.ID] = c.ImageID}}currentContainerID, currentContainerErr := cgroup.CurrentContainerID()@@ -209,6 +207,8 @@ func (s *Service) ServiceUpdateRecords(ctx context.Context, projectIDs []string)// Logs streams Compose logs for the project line by line.func Logs(ctx context.Context, projectName string, logsChan chan<- string, follow bool, tail, since string, timestamps bool) error {+ ctx, cancel := context.WithCancel(ctx)+ defer cancel()pr, pw := io.Pipe()defer func() { _ = pw.Close() }()@@ -218,19 +218,29 @@ func Logs(ctx context.Context, projectName string, logsChan chan<- string, follogo func() {// Closing the read half unblocks a pending pw.Write in ComposeLogs after an abandoned tail// (ctx cancel or bufio.ErrTooLong); otherwise the writer goroutine wedges forever.+ defer cancel()defer func() { _ = pr.CloseWithError(io.ErrClosedPipe) }()- sc := bufio.NewScanner(pr)- sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)- for sc.Scan() {+ reader := bufio.NewReaderSize(pr, 64*1024)+ var line []byte+ for {+ chunk, isPrefix, readErr := reader.ReadLine()+ if readErr != nil {+ done <- readErr+ return+ }+ line = append(line, chunk...)+ if isPrefix && len(line) < 1024*1024 {+ continue+ }select {case <-ctx.Done():done <- ctx.Err()return- case logsChan <- sc.Text():+ case logsChan <- string(line):}+ line = line[:0]}- done <- sc.Err()}()// Writer goroutine: compose logs -> pipediff --git a/backend/internal/project/children/lifecycle/service.go b/backend/internal/project/children/lifecycle/service.goindex a84ef4844..43ea1262b 100644--- a/backend/internal/project/children/lifecycle/service.go+++ b/backend/internal/project/children/lifecycle/service.go@@ -266,6 +266,13 @@ func (s *Service) RunScript(return markTruncated(stdoutBuf), markTruncated(stderrBuf), exitCode, fmt.Errorf("lifecycle container reported error: %s", result.Error.Message)}case waitErr := <-waitResp.Error:+ if waitErr != nil {+ if _, killErr := s.dockerService.Call(context.WithoutCancel(ctx), func(ctx context.Context, dockerClient *client.Client) (client.ContainerKillResult, error) {+ return dockerClient.ContainerKill(ctx, resp.ID, client.ContainerKillOptions{})+ }); killErr != nil && !errdefs.IsNotFound(killErr) {+ slog.WarnContext(ctx, "failed to kill lifecycle container", "containerId", resp.ID, "error", killErr)+ }+ }drainLifecycleLogs(ctx, logsCancel, logs, logDone)if errors.Is(waitErr, context.DeadlineExceeded) || errors.Is(waitCtx.Err(), context.DeadlineExceeded) {return markTruncated(stdoutBuf), markTruncated(stderrBuf), 0, fmt.Errorf("pre-deploy script timed out after %s: %w", timeout, context.DeadlineExceeded)diff --git a/backend/internal/project/children/listing/service.go b/backend/internal/project/children/listing/service.goindex 8c609953b..b8afc4e96 100644--- a/backend/internal/project/children/listing/service.go+++ b/backend/internal/project/children/listing/service.go@@ -190,7 +190,7 @@ func (s *Service) StatusCounts(ctx context.Context, records []project.Record) prcontinue}switch func() string {- if err != nil {+ if err != nil || p.Status == project.StatusDeploying || p.Status == project.StatusRestarting {return p.Status}var services []project.RuntimeService@@ -238,6 +238,8 @@ func row(projectsDir, catalog string, p project.Record, snapshot Snapshot, tags// Status uses the live services, not the possibly stale stored ServiceCount.Status: func() string {switch {+ case p.Status == project.StatusDeploying || p.Status == project.StatusRestarting:+ return p.Statuscase snapshot.Err != nil:return project.StatusUnknowncase len(services) == 0:diff --git a/backend/internal/project/children/sync/service.go b/backend/internal/project/children/sync/service.goindex 398f74c55..f30e43550 100644--- a/backend/internal/project/children/sync/service.go+++ b/backend/internal/project/children/sync/service.go@@ -12,6 +12,7 @@ import ("os""path/filepath""strings"+ "time""github.com/getarcaneapp/arcane/types/v2/project""go.getarcane.app/kit/pkg"@@ -320,6 +321,7 @@ func LoadComposeMetadata(// Load unnamed first so COMPOSE_PROJECT_NAME from .env wins; fall back to// the normalized directory name when that fails.dependencies := project.ComposeDependencies{}+ loadedAt := time.Now()proj, loadErr := projects.LoadComposeProject(ctx, composeFile, "", projectsDirectory, autoInjectEnv, pathMapper, nil, nil, false, &dependencies, nil, nil)if loadErr != nil {dependencies = project.ComposeDependencies{}@@ -337,7 +339,7 @@ func LoadComposeMetadata(}if composeFiles, envFiles, ok := projects.ComposeCacheDependencies(ctx, proj, dependencies, composeFile, projectsDirectory, autoInjectEnv); ok {- if setErr := cache.Set(dirPath, fingerprint, dirPath, projectsDirectory, composeFile, composeFiles, envFiles, meta); setErr != nil {+ if setErr := cache.Set(dirPath, fingerprint, dirPath, projectsDirectory, composeFile, composeFiles, envFiles, loadedAt, meta); setErr != nil {slog.DebugContext(ctx, "failed to cache compose metadata", "path", dirPath, "error", setErr)}}diff --git a/backend/internal/project/children/update/service.go b/backend/internal/project/children/update/service.goindex 8863c1926..208c19e9e 100644--- a/backend/internal/project/children/update/service.go+++ b/backend/internal/project/children/update/service.go@@ -36,6 +36,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/kv""github.com/getarcaneapp/arcane/backend/v2/internal/registry""github.com/getarcaneapp/arcane/backend/v2/pkg/projects"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/imageref")// Service edits Compose sources for image updates and persists rename journals in the KV store.@@ -532,11 +533,15 @@ func EnsureProjectEnvReadable(ctx context.Context, projectsDirectory, projectPat// ImageChanges discovers newer image tags allowed by each service's update// policy. GitOps-managed projects cannot have their source edited.-func (s *Service) ImageChanges(ctx context.Context, effective *types.Project, gitOpsManaged bool) (map[string]updatertypes.ServiceImageChange, error) {+func (s *Service) ImageChanges(ctx context.Context, effective *types.Project, servicesToUpdate []string, gitOpsManaged bool) (map[string]updatertypes.ServiceImageChange, error) {policy := updater.DefaultLabelPolicy()+ selected := projects.NormalizeBuildSelections(servicesToUpdate)changes := make(map[string]updatertypes.ServiceImageChange)for _, key := range effective.ServiceNames() {service := effective.Services[key]+ if !projects.ServiceSelected(selected, key) || imageref.IsUpdateCheckDisabled(service.Labels) {+ continue+ }configuredPolicy := policy.TagPolicy(service.Labels)if service.Build != nil && (configuredPolicy.Strategy == "" || configuredPolicy.Strategy == "auto") && configuredPolicy.Constraint == "" && configuredPolicy.TagPattern == "" {continuediff --git a/backend/internal/project/handler.go b/backend/internal/project/handler.goindex e7195863a..274c34ffe 100644--- a/backend/internal/project/handler.go+++ b/backend/internal/project/handler.go@@ -3,7 +3,6 @@ package projectimport ("context""errors"- "fmt""io""mime/multipart""net/http"@@ -349,10 +348,7 @@ func (h *ProjectHandler) streamProjectOperation(environmentID, projectID string,if err := cfg.Action(opCtx, writer); err != nil {activitylib.FlushWriter(writer)activitylib.CompleteHandlerActivity(runtimeCtx, h.activityService, activityID, cfg.FailureMessage, err)- _, _ = fmt.Fprintf(writer, "{\"error\":%q}\n", err.Error())- if f, ok := writer.(http.Flusher); ok {- f.Flush()- }+ activitylib.WriteErrorLine(rawWriter, err)return}diff --git a/backend/internal/project/helpers.go b/backend/internal/project/helpers.goindex f65c1a1a6..165853175 100644--- a/backend/internal/project/helpers.go+++ b/backend/internal/project/helpers.go@@ -18,6 +18,7 @@ import ("gorm.io/gorm/clause""github.com/getarcaneapp/arcane/backend/v2/internal/settings"+ "github.com/getarcaneapp/arcane/backend/v2/pkg/projects")type builder interface {@@ -37,6 +38,7 @@ type projectMetadataEnv struct {// which resolves the same way as a missing row; sync IDs absent from the// map are queried per project.gitOpsComposePaths map[string]string+ pathMapper func() *projects.PathMapper// composeFiles memoizes successfully resolved compose files by project ID.// Failures are not stored so a later phase in the same request retriesdiff --git a/backend/internal/project/service.go b/backend/internal/project/service.goindex cbb817b09..026ff79ac 100644--- a/backend/internal/project/service.go+++ b/backend/internal/project/service.go@@ -299,6 +299,7 @@ func (s *ProjectService) newProjectMetadataEnv(ctx context.Context, projectsListautoInjectEnv: s.settingsService.GetBoolSetting(ctx, "autoInjectEnv", false),settings: s.settingsService.GetSettingsOrDefaults(ctx),gitOpsComposePaths: make(map[string]string),+ pathMapper: sync.OnceValue(func() *projects.PathMapper { return s.projectPathMapper(ctx) }),composeFiles: make(map[string]string, len(projectsList)),}s.preloadGitOpsComposePaths(ctx, env, projectsList)@@ -582,7 +583,7 @@ func (s *ProjectService) ComposeServiceImage(ctx context.Context, composeName, sif err != nil {return "", "", err}- effective, _, err := s.loadComposeProjectForProject(ctx, proj, nil, serviceName)+ effective, err := s.loadComposeProjectForProject(ctx, proj, nil, serviceName)if err != nil {return "", "", fmt.Errorf("load project %s: %w", proj.Name, err)}@@ -627,7 +628,10 @@ func (s *ProjectService) EnsureProjectPathUnderRoot(ctx context.Context, proj *P}if persist {- if saveErr := s.db.WithContext(ctx).Save(proj).Error; saveErr != nil {+ if saveErr := s.db.WithContext(ctx).Model(&Project{}).Where("id = ?", proj.ID).Updates(map[string]any{+ "path": proj.Path,+ "updated_at": time.Now(),+ }).Error; saveErr != nil {slog.WarnContext(ctx, "failed to persist normalized project path", "error", saveErr)}}@@ -722,10 +726,10 @@ func (s *ProjectService) resolveProjectComposeFile(ctx context.Context, proj *Pr// loadComposeProjectForProject loads the executable compose model for proj. prepare is optional and runs before// host path translation; deployment paths use it to create missing bind directories, read paths pass nil.-func (s *ProjectService) loadComposeProjectForProject(ctx context.Context, proj *Project, prepare projects.PrepareProjectFunc, services ...string) (*types.Project, string, error) {+func (s *ProjectService) loadComposeProjectForProject(ctx context.Context, proj *Project, prepare projects.PrepareProjectFunc, services ...string) (*types.Project, error) {composeFileFullPath, err := s.ResolveProjectComposeFile(ctx, proj)if err != nil {- return nil, "", err+ return nil, err}cfg := s.settingsService.GetSettingsOrDefaults(ctx)@@ -743,9 +747,9 @@ func (s *ProjectService) loadComposeProjectForProject(ctx context.Context, projnil, nil, false, nil, services, prepare,)if loadErr != nil {- return nil, "", loadErr+ return nil, loadErr}- return composeProject, composeFileFullPath, nil+ return composeProject, nil}func (s *ProjectService) getCachedComposeProject(ctx context.Context, proj *Project, env *projectMetadataEnv) (*types.Project, error) {@@ -775,7 +779,12 @@ func (s *ProjectService) getCachedComposeProject(ctx context.Context, proj *Projprojects.NormalizeProjectName(proj.Name),kit.Ternary(dirErr == nil, projectsDirectory, "/app/data/projects"),kit.ParseOrDefault(cfg.AutoInjectEnv.Value, false, strconv.ParseBool),- s.projectPathMapper(ctx),+ func() *projects.PathMapper {+ if env == nil {+ return s.projectPathMapper(ctx)+ }+ return env.pathMapper()+ }(),)}@@ -1048,22 +1057,25 @@ func (s *ProjectService) UpdateProjectServices(ctx context.Context, projectID streturn err}defer unlock()+ return s.updateProjectServices(ctx, projectID, servicesToUpdate, user, discoverTags)+}+func (s *ProjectService) updateProjectServices(ctx context.Context, projectID string, servicesToUpdate []string, user usertypes.Actor, discoverTags bool) error {proj, err := s.getMutableProject(ctx, projectID)if err != nil {return err}if discoverTags {- effective, _, loadErr := s.loadComposeProjectForProject(ctx, proj, nil, servicesToUpdate...)+ effective, loadErr := s.loadComposeProjectForProject(ctx, proj, nil, servicesToUpdate...)if loadErr != nil {return fmt.Errorf("load project for service image checks: %w", loadErr)}- changes, changesErr := s.updates.ImageChanges(ctx, effective, gitOpsSyncID(proj) != "")+ changes, changesErr := s.updates.ImageChanges(ctx, effective, servicesToUpdate, gitOpsSyncID(proj) != "")if changesErr != nil {return changesErr}if len(changes) > 0 {- if _, saveErr := s.SaveProjectServiceImages(ctx, projectID, changes); saveErr != nil {+ if _, saveErr := s.saveProjectServiceImages(ctx, projectID, changes); saveErr != nil {return saveErr}}@@ -1072,14 +1084,14 @@ func (s *ProjectService) UpdateProjectServices(ctx context.Context, projectID st// 1. Load projectprepare := deployment.PrepareProjectBindDirectories(proj.Path)- compProj, _, err := s.loadComposeProjectForProject(ctx, proj, prepare, servicesToUpdate...)+ compProj, err := s.loadComposeProjectForProject(ctx, proj, prepare, servicesToUpdate...)if err != nil {return fmt.Errorf("failed to load compose project: %w", err)}dependents, stoppedDependents := s.deployment.NamespaceDependents(ctx, compProj, servicesToUpdate)if len(dependents)+len(stoppedDependents) > 0 {slog.InfoContext(ctx, "recreating namespace dependents with updated services", "projectId", projectID, "services", servicesToUpdate, "dependents", dependents, "stoppedDependents", stoppedDependents)- if compProj, _, err = s.loadComposeProjectForProject(ctx, proj, prepare, slices.Concat(servicesToUpdate, dependents, stoppedDependents)...); err != nil {+ if compProj, err = s.loadComposeProjectForProject(ctx, proj, prepare, slices.Concat(servicesToUpdate, dependents, stoppedDependents)...); err != nil {return fmt.Errorf("failed to load compose project with dependents: %w", err)}}@@ -1093,7 +1105,7 @@ func (s *ProjectService) UpdateProjectServices(ctx context.Context, projectID stcredentials, err := s.ResolveRegistryCredentials(ctx)if err != nil {- if statusErr := s.updateProjectStatus(ctx, projectID, previousStatus); statusErr != nil {+ if statusErr := s.updateProjectStatus(context.WithoutCancel(ctx), projectID, previousStatus); statusErr != nil {slog.ErrorContext(ctx, "UpdateProjectServices: failed to restore project status after credential lookup failure", "projectId", projectID, "error", statusErr)}return fmt.Errorf("resolve registry credentials: %w", err)@@ -1121,7 +1133,7 @@ func (s *ProjectService) UpdateProjectServices(ctx context.Context, projectID st}// 6. Finalize status- if updateProjectStatusandCountsErr := s.updateProjectStatusAndCounts(ctx, projectID, ProjectStatusRunning); updateProjectStatusandCountsErr != nil {+ if updateProjectStatusandCountsErr := s.updateProjectStatusAndCounts(context.WithoutCancel(ctx), projectID, ProjectStatusRunning); updateProjectStatusandCountsErr != nil {return updateProjectStatusandCountsErr}@@ -1143,6 +1155,12 @@ func (s *ProjectService) UpdateProjectServices(ctx context.Context, projectID st}func (s *ProjectService) ArchiveProject(ctx context.Context, projectID string, user usertypes.Actor) error {+ unlock, err := s.lockProject(ctx, "archive project", projectID)+ if err != nil {+ return err+ }+ defer unlock()+proj, err := s.GetProjectFromDatabaseByID(ctx, projectID)if err != nil {return err@@ -1182,6 +1200,12 @@ func (s *ProjectService) ArchiveProject(ctx context.Context, projectID string, u}func (s *ProjectService) UnarchiveProject(ctx context.Context, projectID string, user usertypes.Actor) error {+ unlock, err := s.lockProject(ctx, "unarchive project", projectID)+ if err != nil {+ return err+ }+ defer unlock()+proj, err := s.GetProjectFromDatabaseByID(ctx, projectID)if err != nil {return err@@ -1253,7 +1277,7 @@ func (s *ProjectService) deployProject(ctx context.Context, projectID string, usreturn s.lifecycleService.runPreDeploy(ctx, projectFromDb, user)},Load: func(ctx context.Context) (*types.Project, error) {- model, _, loadComposeProjectForProjectErr := s.loadComposeProjectForProject(ctx, projectFromDb, deployment.PrepareProjectBindDirectories(projectFromDb.Path))+ model, loadComposeProjectForProjectErr := s.loadComposeProjectForProject(ctx, projectFromDb, deployment.PrepareProjectBindDirectories(projectFromDb.Path))if loadComposeProjectForProjectErr == nil {closeSuppression = s.eventService.BeginComposeSuppressionWindow(model.Name)}@@ -1285,7 +1309,7 @@ func (s *ProjectService) deployProject(ctx context.Context, projectID string, us"could not log project deployment action",)- err = s.updateProjectStatusAndCounts(ctx, projectID, ProjectStatusRunning)+ err = s.updateProjectStatusAndCounts(context.WithoutCancel(ctx), projectID, ProjectStatusRunning)if err != nil {slog.ErrorContext(ctx, "failed to update project status and counts after deploy", "projectId", projectID, "error", err)}@@ -1309,7 +1333,7 @@ func (s *ProjectService) DownProject(ctx context.Context, projectID string, user// downProject brings the project down and restores its previous status on failure.func (s *ProjectService) downProject(ctx context.Context, projectFromDb *Project, user usertypes.Actor) error {projectID := projectFromDb.ID- proj, _, lerr := s.loadComposeProjectForProject(ctx, projectFromDb, nil)+ proj, lerr := s.loadComposeProjectForProject(ctx, projectFromDb, nil)if lerr != nil {return fmt.Errorf("failed to load compose project: %w", lerr)}@@ -1321,7 +1345,7 @@ func (s *ProjectService) downProject(ctx context.Context, projectFromDb *Projectdefer s.eventService.BeginComposeSuppressionWindow(proj.Name)()if composeDownErr := projects.ComposeDown(ctx, proj, false); composeDownErr != nil {- _ = s.updateProjectStatus(ctx, projectID, projectFromDb.Status)+ _ = s.updateProjectStatus(context.WithoutCancel(ctx), projectID, projectFromDb.Status)return fmt.Errorf("failed to bring down project: %w", composeDownErr)}@@ -1331,7 +1355,7 @@ func (s *ProjectService) downProject(ctx context.Context, projectFromDb *Project"projectName": projectFromDb.Name,}, "could not log project down action")- return s.updateProjectStatusAndCounts(ctx, projectID, ProjectStatusStopped)+ return s.updateProjectStatusAndCounts(context.WithoutCancel(ctx), projectID, ProjectStatusStopped)}// CreateProject creates a project's directory, files, and DB row. A directory@@ -1368,6 +1392,9 @@ func (s *ProjectService) CreateProject(// cannot claim the directory while it is half written.s.syncMu.Lock()defer s.syncMu.Unlock()+ if nameErr := s.ensureProjectNameAvailable(ctx, name, ""); nameErr != nil {+ return nil, nameErr+ }basePath := filepath.Join(projectsDirectory, projects.SanitizeProjectName(name))projectPath, folderName, reused, err := kit.Ternary(allowNameSuffix, func() (string, string, bool, error) {@@ -1552,7 +1579,7 @@ func (s *ProjectService) DestroyProject(ctx context.Context, projectID string, r}if removeVolumes {- compProj, _, lerr := s.loadComposeProjectForProject(ctx, proj, nil)+ compProj, lerr := s.loadComposeProjectForProject(ctx, proj, nil)switch {case lerr == nil:defer s.eventService.BeginComposeSuppressionWindow(compProj.Name)()@@ -1644,6 +1671,9 @@ func (s *ProjectService) RedeployProject(ctx context.Context, projectID string,}if pullProjectImagesErr := s.PullProjectImages(ctx, projectID, kit.Ternary(progressWriter == nil, io.Discard, progressWriter), user, credentials); pullProjectImagesErr != nil {slog.WarnContext(ctx, "failed to pull project images", "error", pullProjectImagesErr)+ logWriter := docker.NewLogLineWriter(progressWriter)+ _, _ = io.WriteString(logWriter, "WARNING: failed to pull project images; deploying with local images:\n"+pullProjectImagesErr.Error()+"\n")+ _ = logWriter.Close()}return s.deployProject(ctx, projectID, user, options)@@ -1655,13 +1685,14 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID stringreturn err}- compProj, _, lerr := s.loadComposeProjectForProject(ctx, proj, nil)+ compProj, lerr := s.loadComposeProjectForProject(ctx, proj, nil)if lerr != nil {return fmt.Errorf("failed to load compose project: %w", lerr)}defer s.eventService.BeginComposeSuppressionWindow(compProj.Name)()+ var pullErrs []errorfor _, imageRef := range projects.PullableImageRefs(compProj) {if pullErr := s.imageService.Pull(ctx, image.PullRequest{Ref: imageRef,@@ -1669,10 +1700,10 @@ func (s *ProjectService) PullProjectImages(ctx context.Context, projectID stringUser: user,Credentials: credentials,}); pullErr != nil {- return pullErr+ pullErrs = append(pullErrs, fmt.Errorf("pull %s: %w", imageRef, pullErr))}}- return nil+ return errors.Join(pullErrs...)}func (s *ProjectService) BuildProjectServices(ctx context.Context, projectID string, options projecttypes.BuildOptions, progressWriter io.Writer, user *usertypes.Actor) error {@@ -1681,7 +1712,7 @@ func (s *ProjectService) BuildProjectServices(ctx context.Context, projectID strreturn err}- projectModel, _, derr := s.loadComposeProjectForProject(ctx, projectFromDb, nil)+ projectModel, derr := s.loadComposeProjectForProject(ctx, projectFromDb, nil)if derr != nil {return fmt.Errorf("failed to load compose project in %s: %w", projectFromDb.Path, derr)}@@ -1706,7 +1737,7 @@ func (s *ProjectService) RestartProject(ctx context.Context, projectID string, sreturn resolveProjectComposeFileErr}- compProj, _, lerr := s.loadComposeProjectForProject(ctx, proj, nil)+ compProj, lerr := s.loadComposeProjectForProject(ctx, proj, nil)if lerr != nil {return fmt.Errorf("failed to load compose project: %w", lerr)}@@ -1718,7 +1749,7 @@ func (s *ProjectService) RestartProject(ctx context.Context, projectID string, sdefer s.eventService.BeginComposeSuppressionWindow(compProj.Name)()if composeRestartErr := projects.ComposeRestart(ctx, compProj, services); composeRestartErr != nil {- _ = s.updateProjectStatus(ctx, projectID, proj.Status)+ _ = s.updateProjectStatus(context.WithoutCancel(ctx), projectID, proj.Status)return fmt.Errorf("failed to restart project: %w", composeRestartErr)}@@ -1732,7 +1763,7 @@ func (s *ProjectService) RestartProject(ctx context.Context, projectID string, s}s.logProjectEvent(ctx, event.EventTypeProjectStart, projectID, proj.Name, user, metadata, "could not log project restart action")- return s.updateProjectStatusAndCounts(ctx, projectID, ProjectStatusRunning)+ return s.updateProjectStatusAndCounts(context.WithoutCancel(ctx), projectID, ProjectStatusRunning)}func (s *ProjectService) updateProjectStatusAndCounts(ctx context.Context, projectID string, status ProjectStatus) error {@@ -1756,14 +1787,15 @@ func (s *ProjectService) updateProjectStatusAndCounts(ctx context.Context, proje// restoreRuntimeProjectStatus re-derives status from the running services after a failed deploy, falling back to Stopped.func (s *ProjectService) restoreRuntimeProjectStatus(ctx context.Context, projectID string) {- services, err := s.projectServices(ctx, projectID)+ restoreCtx := context.WithoutCancel(ctx)+ services, err := s.projectServices(restoreCtx, projectID)if err == nil {- if err = s.updateProjectStatusAndCounts(ctx, projectID, ProjectStatus(listing.ProjectStatus(services))); err == nil {+ if err = s.updateProjectStatusAndCounts(restoreCtx, projectID, ProjectStatus(listing.ProjectStatus(services))); err == nil {return}}slog.WarnContext(ctx, "failed to restore project status after deploy failure", "projectId", projectID, "error", err)- if updateErr := s.updateProjectStatus(ctx, projectID, ProjectStatusStopped); updateErr != nil {+ if updateErr := s.updateProjectStatus(restoreCtx, projectID, ProjectStatusStopped); updateErr != nil {slog.WarnContext(ctx, "failed to set stopped status after deploy failure", "projectId", projectID, "error", updateErr)}}@@ -2235,7 +2267,7 @@ func buildUpdateInfoSummary(}func (s *ProjectService) CountServicesFromCompose(ctx context.Context, p Project) (int, error) {- proj, _, err := s.loadComposeProjectForProject(ctx, &p, nil)+ proj, err := s.loadComposeProjectForProject(ctx, &p, nil)if err != nil {return 0, err}@@ -2464,6 +2496,7 @@ func (s *ProjectService) ProjectMetadata(ctx context.Context, p Project, env *prreturn cached}+ loadedAt := time.Now()meta, err := projects.ParseArcaneComposeMetadata(ctx, composeFile, env.projectsDirectory, env.autoInjectEnv)if err != nil {slog.WarnContext(ctx, "failed to parse Arcane compose metadata", "path", composeFile, "error", err)@@ -2473,7 +2506,7 @@ func (s *ProjectService) ProjectMetadata(ctx context.Context, p Project, env *prif p.ID == "" {return meta}- if setErr := s.metaCache.Set(p.ID, fingerprint, p.Path, env.projectsDirectory, composeFile, meta.ComposeFiles, meta.EnvFiles, meta); setErr != nil {+ if setErr := s.metaCache.Set(p.ID, fingerprint, p.Path, env.projectsDirectory, composeFile, meta.ComposeFiles, meta.EnvFiles, loadedAt, meta); setErr != nil {slog.DebugContext(ctx, "failed to cache Compose metadata", "projectId", p.ID, "error", setErr)}@@ -2983,7 +3016,8 @@ func (s *ProjectService) applyProjectUpdate(}()// A rename moves a stopped project's directory to its sanitized new name under the projects directory.- if newName != "" && proj.Name != newName {+ renamed := newName != "" && proj.Name != newName+ if renamed {newDirName := projects.SanitizeProjectName(newName)targetPath := filepath.Clean(filepath.Join(projectsDirectory, newDirName))if filepath.Clean(proj.Path) != targetPath {@@ -3029,7 +3063,23 @@ func (s *ProjectService) applyProjectUpdate(if migrationErr := projects.ApplyRenameVolumeMigration(ctx, s.updates.Operations(), volumeMigration, renameJournal, &volumeMigrationApplied); migrationErr != nil {return migrationErr}- if saveErr := s.db.WithContext(ctx).Save(proj).Error; saveErr != nil {+ if saveErr := s.db.WithContext(ctx).Model(&Project{}).Where("id = ?", proj.ID).Updates(func() map[string]any {+ columns := map[string]any{+ "name": proj.Name,+ "dir_name": proj.DirName,+ "path": proj.Path,+ "updated_at": time.Now(),+ }+ if renamed {+ maps.Copy(columns, map[string]any{+ "status": proj.Status,+ "status_reason": proj.StatusReason,+ "service_count": proj.ServiceCount,+ "running_count": proj.RunningCount,+ })+ }+ return columns+ }()).Error; saveErr != nil {return fmt.Errorf("failed to update project: %w", saveErr)}projects.FinalizeRenameAfterCommit(ctx, s.updates.Operations(), proj.ID, volumeMigration, renameJournal, new(renameJournal != nil))@@ -3099,7 +3149,10 @@ func (s *ProjectService) ApplyGitSyncProjectFiles(if overrideErr := projects.WriteComposeOverrideFile(ctx, projectsDirectory, proj.Path, gitOverrideContent, gitOverrideFileName); overrideErr != nil {return fmt.Errorf("failed to sync git override file: %w", overrideErr)}- if saveErr := s.db.WithContext(ctx).Save(proj).Error; saveErr != nil {+ if saveErr := s.db.WithContext(ctx).Model(&Project{}).Where("id = ?", proj.ID).Updates(map[string]any{+ "path": proj.Path,+ "updated_at": time.Now(),+ }).Error; saveErr != nil {return fmt.Errorf("failed to update project: %w", saveErr)}return nil@@ -3143,6 +3196,17 @@ func (s *ProjectService) ApplyGitSyncProjectFiles(return proj, contentChanged, nil}+func (s *ProjectService) ensureProjectNameAvailable(ctx context.Context, name, excludeProjectID string) error {+ var others []Project+ if err := s.db.WithContext(ctx).Select("id", "name", "compose_project_name").Where("id <> ?", excludeProjectID).Find(&others).Error; err != nil {+ return fmt.Errorf("failed to check existing project names: %w", err)+ }+ if _, taken := listing.KnownComposeProjectNames(projectRecords(others))[projects.NormalizeProjectName(name)]; taken {+ return common.Classify(common.ErrProjectWorkspaceConflict, fmt.Errorf("a project named %q already exists", name))+ }+ return nil+}+// prepareProjectRenameVolumeMigration rejects an unusable rename target, then plans the volume rename for a stopped// project. Content changes are applied to a scratch preview first so the plan reflects the updated compose files.func (s *ProjectService) prepareProjectRenameVolumeMigration(@@ -3155,6 +3219,9 @@ func (s *ProjectService) prepareProjectRenameVolumeMigration(if newDirName == "" || strings.Trim(newDirName, "_") == "" {return nil, errors.New("invalid project name: results in empty directory name")}+ if nameErr := s.ensureProjectNameAvailable(ctx, newName, proj.ID); nameErr != nil {+ return nil, nameErr+ }if targetPath := filepath.Clean(filepath.Join(projectsDirectory, newDirName)); filepath.Clean(proj.Path) != targetPath {targetLogical, logicalErr := acfs.LogicalPath(projectsDirectory, targetPath)if logicalErr != nil {@@ -3214,7 +3281,7 @@ func (s *ProjectService) prepareProjectRenameVolumeMigration(return nil, nil}- composeProject, _, err := s.loadComposeProjectForProject(ctx, target, nil)+ composeProject, err := s.loadComposeProjectForProject(ctx, target, nil)if err != nil {if errors.Is(err, common.ErrProjectComposeFileNotFound) {return nil, nil@@ -3232,18 +3299,33 @@ func (s *ProjectService) prepareProjectRenameVolumeMigration(// UpdateProjectServiceImages persists selected service image tags before recreating them.func (s *ProjectService) UpdateProjectServiceImages(ctx context.Context, projectID string, changes map[string]updatertypes.ServiceImageChange, user usertypes.Actor) error {- services, err := s.SaveProjectServiceImages(ctx, projectID, changes)+ unlock, err := s.lockProject(ctx, "update project service images", projectID)+ if err != nil {+ return err+ }+ defer unlock()++ services, err := s.saveProjectServiceImages(ctx, projectID, changes)if err != nil {return err}// Keep the desired source on deployment failure: Compose may have partially// recreated services, and the pending update must remain retryable.- return s.UpdateProjectServices(ctx, projectID, services, user, false)+ return s.updateProjectServices(ctx, projectID, services, user, false)}// SaveProjectServiceImages persists selected service image tags without// deploying them and returns the changed services.func (s *ProjectService) SaveProjectServiceImages(ctx context.Context, projectID string, changes map[string]updatertypes.ServiceImageChange) ([]string, error) {+ unlock, err := s.lockProject(ctx, "save project service images", projectID)+ if err != nil {+ return nil, err+ }+ defer unlock()+ return s.saveProjectServiceImages(ctx, projectID, changes)+}++func (s *ProjectService) saveProjectServiceImages(ctx context.Context, projectID string, changes map[string]updatertypes.ServiceImageChange) ([]string, error) {if len(changes) == 0 {return nil, errors.New("service image changes are required")}@@ -3255,7 +3337,7 @@ func (s *ProjectService) SaveProjectServiceImages(ctx context.Context, projectIDreturn nil, errors.New("tag updates cannot edit a GitOps-managed project; update image tags in the source repository")}// Selecting the changed services keeps profile-gated ones active.- effective, _, err := s.loadComposeProjectForProject(ctx, proj, nil, slices.Collect(maps.Keys(changes))...)+ effective, err := s.loadComposeProjectForProject(ctx, proj, nil, slices.Collect(maps.Keys(changes))...)if err != nil {return nil, fmt.Errorf("load project for tag update: %w", err)}@@ -3383,15 +3465,15 @@ func (s *ProjectService) projectServices(ctx context.Context, projectID string)return nil, err}- composeProject, composeFileFullPath, derr := s.loadComposeProjectForProject(ctx, projectFromDb, nil)+ containers, listErr := s.details.ComposeContainers(ctx)+ matched := listing.ProjectContainers(projectRecord(*projectFromDb), listing.GroupComposeContainersByProject(containers))+ composeProject, derr := s.getCachedComposeProject(ctx, projectFromDb, nil)if errors.Is(derr, common.ErrProjectEnvUnreadable) {// The Compose file cannot be loaded, so derive services from labeled containers.- containers, listErr := s.details.ComposeContainers(ctx)if listErr != nil {return nil, listErr}meta := s.ProjectMetadata(ctx, *projectFromDb, nil)- matched := listing.ProjectContainers(projectRecord(*projectFromDb), listing.GroupComposeContainersByProject(containers))currentContainerID, currentContainerErr := cgroup.CurrentContainerID()services := make([]projecttypes.RuntimeService, 0, len(matched))for _, c := range matched {@@ -3402,13 +3484,11 @@ func (s *ProjectService) projectServices(ctx context.Context, projectID string)if derr != nil {return []projecttypes.RuntimeService{}, fmt.Errorf("failed to load compose project in %s: %w", projectFromDb.Path, derr)}-- projectsDirectory, projectsDirErr := s.GetProjectsDirectory(ctx)- if projectsDirErr != nil {- slog.WarnContext(ctx, "failed to resolve projects directory for Arcane compose metadata", "path", composeFileFullPath, "error", projectsDirErr)+ if listErr != nil {+ slog.WarnContext(ctx, "failed to list containers for project image identity", "projectName", composeProject.Name, "error", listErr)}- autoInjectEnv := s.settingsService.GetBoolSetting(ctx, "autoInjectEnv", false)- return s.details.ComposeServices(ctx, composeProject, composeFileFullPath, projectsDirectory, autoInjectEnv, IconCatalogForContext(ctx))++ return s.details.ComposeServices(ctx, composeProject, matched, s.ProjectMetadata(ctx, *projectFromDb, nil), IconCatalogForContext(ctx))}func (s *ProjectService) StreamProjectLogs(ctx context.Context, projectID string, logsChan chan<- string, follow bool, tail, since string, timestamps bool) error {@@ -3416,7 +3496,12 @@ func (s *ProjectService) StreamProjectLogs(ctx context.Context, projectID stringif err != nil {return err}- return projectdetails.Logs(ctx, proj.Name, logsChan, follow, tail, since, timestamps)+ containers, listErr := s.details.ComposeContainers(ctx)+ if listErr != nil {+ slog.WarnContext(ctx, "failed to list containers for project log stream", "projectId", projectID, "error", listErr)+ }+ matched := listing.ProjectContainers(projectRecord(*proj), listing.GroupComposeContainersByProject(containers))+ return projectdetails.Logs(ctx, projectdetails.RuntimeProjectName(proj.Name, matched), logsChan, follow, tail, since, timestamps)}func (s *ProjectService) GetProjectStatusCounts(ctx context.Context) (projecttypes.StatusCounts, error) {diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.goindex 619d0b656..79b3250f8 100644--- a/backend/internal/project/service_test.go+++ b/backend/internal/project/service_test.go@@ -2974,6 +2974,17 @@ func TestProjectService_CreateProject_CommitsWorkspaceAndConfigurationTogether(tcontent, err := os.ReadFile(filepath.Join(created.Path, "config", "app.txt"))require.NoError(t, err)require.Equal(t, "workspace content\n", string(content))++ compose := "services:\n app:\n image: nginx:alpine\n"+ user := usertypes.Actor{ID: "u1", Username: "tester"}+ _, err = svc.CreateProject(ctx, "Atomic-Workspace", compose, nil, projecttypes.CreateProjectWorkspaceManifest{}, nil, nil, nil, user)+ require.ErrorIs(t, err, common.ErrConflict)+ _, err = svc.CreateProject(ctx, "atomic-workspace", compose, nil, projecttypes.CreateProjectWorkspaceManifest{}, nil, nil, nil, user, false)+ require.ErrorIs(t, err, common.ErrConflict)+ var count int64+ require.NoError(t, db.WithContext(ctx).Model(&Project{}).Count(&count).Error)+ require.Equal(t, int64(1), count)+ require.NoDirExists(t, filepath.Join(projectsDir, "Atomic-Workspace"))}func TestProjectService_CreateProject_RollsBackInvalidWorkspaceManifest(t *testing.T) {@@ -5297,7 +5308,7 @@ func TestProjectService_DeployProject_BuildsGeneratedImageWithoutPull(t *testingdb,settingsService,event.NewEventService(db, nil, nil),- image.NewImageService(db, nil, nil, nil, nil, nil, nil, nil),+ image.NewImageService(db, testDockerService, nil, nil, nil, nil, nil, nil),testDockerService,buildSvc,nil,@@ -9264,7 +9275,7 @@ func TestProjectServiceManualUpdateRejectsUnsafeTagPolicies(t *testing.T) {serviceConfig := types.ServiceConfig{Image: "app:1.2.0", Labels: types.Labels{labels.LabelUpdateStrategy: "tag"}}tt.change(&serviceConfig)service := NewProjectService(nil, nil, nil, nil, testDockerService, testBuildBuilder{}, nil, nil, nil, nil, testRegistryCredentials)- changes, err := service.updates.ImageChanges(t.Context(), &types.Project{Services: types.Services{"app": serviceConfig}}, false)+ changes, err := service.updates.ImageChanges(t.Context(), &types.Project{Services: types.Services{"app": serviceConfig}}, nil, false)require.Error(t, err)require.NotContains(t, err.Error(), "registry service unavailable")require.Nil(t, changes)@@ -9272,7 +9283,7 @@ func TestProjectServiceManualUpdateRejectsUnsafeTagPolicies(t *testing.T) {}local := types.ServiceConfig{Name: "app", Image: "app:1.2.0", Build: &types.BuildConfig{Context: "."}}emptyService := NewProjectService(nil, nil, nil, nil, testDockerService, testBuildBuilder{}, nil, nil, nil, nil, testRegistryCredentials)- changes, err := emptyService.updates.ImageChanges(t.Context(), &types.Project{Services: types.Services{"app": local}}, false)+ changes, err := emptyService.updates.ImageChanges(t.Context(), &types.Project{Services: types.Services{"app": local}}, nil, false)require.NoError(t, err)require.Empty(t, changes, "automatic inference must preserve local-build handling")}diff --git a/backend/internal/updater/children/execution/service.go b/backend/internal/updater/children/execution/service.goindex 79356d5eb..45f40f524 100644--- a/backend/internal/updater/children/execution/service.go+++ b/backend/internal/updater/children/execution/service.go@@ -4,6 +4,7 @@ import ("context""encoding/json/v2""errors"+ "fmt""slices""time"@@ -37,6 +38,7 @@ type (RecordUpdate func(ctx context.Context, activityID string, result *updater.Result, runErr error) scheduler.OutcomeFreezeContainer func(ctx context.Context, containerID string) (*updater.FrozenUpdateTarget, error)ConfirmTarget func(ctx context.Context, target updater.FrozenUpdateTarget) (bool, bool, error)+ ResumeTarget func(ctx context.Context, target updater.FrozenUpdateTarget) (updater.FrozenUpdateTarget, bool, error)WithFrozenTarget func(ctx context.Context, target *updater.FrozenUpdateTarget, persist func() error) context.Context}@@ -147,8 +149,18 @@ func (s *Service) update(ctx context.Context, t flow.Task) (any, error) {}},}, nil), nilcase !unchanged:- return failed("Interrupted container update effect cannot be confirmed"), nil+ return failed(fmt.Sprintf(+ "Interrupted container update effect cannot be confirmed: recreate %s from %s (previous image %s)",+ target.ContainerName,+ target.DesiredImageRef,+ target.BaselineImageID,+ )), nil}+ resumed, _, resumeErr := s.deps.ResumeTarget(ctx, target)+ if resumeErr != nil {+ return nil, resumeErr+ }+ target = resumed} else {frozen, freezeErr := s.deps.FreezeContainer(ctx, input.ContainerID)if freezeErr != nil {diff --git a/backend/internal/updater/children/recovery/service.go b/backend/internal/updater/children/recovery/service.goindex 87fc69290..73bf5b525 100644--- a/backend/internal/updater/children/recovery/service.go+++ b/backend/internal/updater/children/recovery/service.go@@ -127,6 +127,7 @@ func (s *Service) FreezePending(ctx context.Context, run scheduler.Run, excludedreturn !record.HasUpdate || excluded[refs.NormalizeImageUpdateRef(record.ImageRef())] || excluded[record.ContainerID]})failures := map[string]error{}+ digests := map[string]string{}selected := make([]updater.ImageUpdateRecord, 0, len(records))var targets []arcaneupdater.FrozenUpdateTargetif len(records) > 0 {@@ -155,7 +156,7 @@ func (s *Service) FreezePending(ctx context.Context, run scheduler.Run, excludedif deferred(record, candidate) {continue}- target, frozenRecord, freezeRecordTargetErr := s.freezeRecordTarget(ctx, record, candidate.ID)+ target, frozenRecord, freezeRecordTargetErr := s.freezeRecordTarget(ctx, record, candidate.ID, digests)if freezeRecordTargetErr != nil {failures[candidate.ID] = freezeRecordTargetErrcontinue@@ -204,7 +205,12 @@ func (s *Service) FreezePending(ctx context.Context, run scheduler.Run, excludedreturn context.WithValue(ctx, frozenPendingKey{}, &plan), nil}-func (s *Service) freezeRecordTarget(ctx context.Context, record updater.ImageUpdateRecord, containerID string) (*arcaneupdater.FrozenUpdateTarget, updater.ImageUpdateRecord, error) {+func (s *Service) freezeRecordTarget(+ ctx context.Context,+ record updater.ImageUpdateRecord,+ containerID string,+ digests map[string]string,+) (*arcaneupdater.FrozenUpdateTarget, updater.ImageUpdateRecord, error) {frozen, err := s.FreezeContainer(ctx, containerID)if err != nil {return nil, record, err@@ -213,6 +219,9 @@ func (s *Service) freezeRecordTarget(ctx context.Context, record updater.ImageUpif pinned := kit.Ternary(record.IsTagUpdate(), "", kit.FromPtr(record.LatestDigest)); pinned != "" {return pinned, nil}+ if resolved, ok := digests[record.NewImageRef()]; ok {+ return resolved, nil+ }return s.images.RemoteDigest(ctx, record.NewImageRef(), nil)}()if err != nil {@@ -221,6 +230,7 @@ func (s *Service) freezeRecordTarget(ctx context.Context, record updater.ImageUpif digest == "" {return nil, record, errors.New("registry returned no desired digest")}+ digests[record.NewImageRef()] = digestreturn &arcaneupdater.FrozenUpdateTarget{ //exhaustruct:enforceContainerID: frozen.ContainerID,ContainerName: frozen.ContainerName,@@ -230,6 +240,7 @@ func (s *Service) freezeRecordTarget(ctx context.Context, record updater.ImageUpBaselineImageID: frozen.BaselineImageID,BaselineStartedAt: frozen.BaselineStartedAt,BaselineRestartCount: frozen.BaselineRestartCount,+ BaselineRunning: frozen.BaselineRunning,DesiredImageRef: record.NewImageRef(),DesiredDigest: digest,},@@ -266,9 +277,42 @@ func (s *Service) FreezeContainer(ctx context.Context, id string) (*arcaneupdateBaselineImageID: result.Container.Image,BaselineStartedAt: kit.FromPtr(result.Container.State).StartedAt,BaselineRestartCount: result.Container.RestartCount,+ BaselineRunning: kit.FromPtr(result.Container.State).Running,}, nil}+func (s *Service) ResumeTarget(ctx context.Context, target arcaneupdater.FrozenUpdateTarget) (arcaneupdater.FrozenUpdateTarget, bool, error) {+ current, err := s.FreezeContainer(ctx, target.ContainerID)+ if err != nil {+ return target, false, err+ }+ if !target.BaselineRunning || current.BaselineRunning {+ return target, false, nil+ }+ if _, startErr := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerStartResult, error) {+ return dockerClient.ContainerStart(ctx, target.ContainerID, client.ContainerStartOptions{})+ }); startErr != nil {+ return target, false, startErr+ }+ started, err := s.FreezeContainer(ctx, target.ContainerID)+ if err != nil {+ return target, false, err+ }+ return arcaneupdater.FrozenUpdateTarget{ //exhaustruct:enforce+ ContainerID: target.ContainerID,+ ContainerName: target.ContainerName,+ ComposeProject: target.ComposeProject,+ ComposeService: target.ComposeService,+ ComposeNumber: target.ComposeNumber,+ BaselineImageID: target.BaselineImageID,+ BaselineStartedAt: started.BaselineStartedAt,+ BaselineRestartCount: started.BaselineRestartCount,+ BaselineRunning: target.BaselineRunning,+ DesiredImageRef: target.DesiredImageRef,+ DesiredDigest: target.DesiredDigest,+ }, true, nil+}+// PreparePull commits the exact image before the engine changes Docker.func (s *Service) PreparePull(ctx context.Context, imageRef string) (string, error) {if single, ok := ctx.Value(frozenSingleKey{}).(*frozenSingle); ok {@@ -286,6 +330,7 @@ func (s *Service) PreparePull(ctx context.Context, imageRef string) (string, errBaselineImageID: single.Target.BaselineImageID,BaselineStartedAt: single.Target.BaselineStartedAt,BaselineRestartCount: single.Target.BaselineRestartCount,+ BaselineRunning: single.Target.BaselineRunning,DesiredImageRef: imageRef,DesiredDigest: digest,}@@ -370,7 +415,17 @@ func (s *Service) ConfirmTarget(ctx context.Context, target arcaneupdater.Frozenreturn false, false, nil}if desiredID != "" && result.Container.Image == desiredID {- return true, false, nil+ switch {+ case !target.BaselineRunning || kit.FromPtr(result.Container.State).Running:+ return true, false, nil+ case result.Container.ID != target.ContainerID && kit.FromPtr(result.Container.State).Status == container.StateCreated:+ if _, startErr := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.ContainerStartResult, error) {+ return dockerClient.ContainerStart(ctx, result.Container.ID, client.ContainerStartOptions{})+ }); startErr != nil {+ return false, false, startErr+ }+ return true, false, nil+ }}unchanged := result.Container.ID == target.ContainerID &&result.Container.Image == target.BaselineImageID &&@@ -445,7 +500,7 @@ func (s *Service) ResumePlan(ctx context.Context, run scheduler.Run) (context.Corecords []updater.ImageUpdateRecordunresolved bool)- for _, target := range plan.Targets {+ for position, target := range plan.Targets {if slices.ContainsFunc(run.Outcome.Targets, func(outcome scheduler.TargetOutcome) bool {return outcome.ID == target.ContainerID && (outcome.Status == scheduler.Succeeded || outcome.Status == scheduler.Skipped)}) {@@ -456,7 +511,27 @@ func (s *Service) ResumePlan(ctx context.Context, run scheduler.Run) (context.Coreturn ctx, 0, false, confirmErr}if !confirmed && unchanged {- targets = append(targets, target)+ resumed, started, resumeErr := s.ResumeTarget(ctx, target)+ if resumeErr != nil {+ return ctx, 0, false, resumeErr+ }+ plan.Targets[position] = resumed+ if started {+ raw, marshalErr := json.Marshal(plan)+ if marshalErr != nil {+ return ctx, 0, false, marshalErr+ }+ if progressErr := jobcontext.Progress(ctx, scheduler.TargetOutcome{+ ID: "auto-update",+ ResourceType: "update-batch",+ Status: scheduler.Running,+ RecoveryData: raw,+ ActivityID: s.activityID(ctx),+ }); progressErr != nil {+ return ctx, 0, false, progressErr+ }+ }+ targets = append(targets, resumed)if index := slices.IndexFunc(plan.Records, func(record updater.ImageUpdateRecord) bool { return record.ContainerID == target.ContainerID }); index >= 0 {records = append(records, plan.Records[index])}@@ -467,7 +542,11 @@ func (s *Service) ResumePlan(ctx context.Context, run scheduler.Run) (context.CoID: target.ContainerID,ResourceType: "container",Status: kit.Ternary(confirmed, scheduler.Succeeded, scheduler.NeedsAttention),- Message: kit.Ternary(confirmed, "Frozen desired image confirmed", "Update effect could not be confirmed"),+ Message: kit.Ternary(+ confirmed,+ "Frozen desired image confirmed",+ "Update effect could not be confirmed: recreate "+target.ContainerName+" from "+target.DesiredImageRef+" (previous image "+target.BaselineImageID+")",+ ),}); progressErr != nil {return ctx, 0, false, progressErr}diff --git a/backend/internal/updater/children/recovery/service_test.go b/backend/internal/updater/children/recovery/service_test.goindex b4fc46b65..98bd4a580 100644--- a/backend/internal/updater/children/recovery/service_test.go+++ b/backend/internal/updater/children/recovery/service_test.go@@ -7,6 +7,7 @@ import ("net/http""net/http/httptest""strings"+ "sync/atomic""testing""github.com/getarcaneapp/arcane/types/v2/containerregistry"@@ -17,6 +18,7 @@ import ("github.com/moby/moby/client""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"+ kit "go.getarcane.app/kit/pkg""go.getarcane.app/updater""github.com/getarcaneapp/arcane/backend/v2/internal/docker"@@ -437,3 +439,179 @@ func TestResumePlanConfirmsRedeliveredChangeWithoutReapplying(t *testing.T) {})}}++func TestConfirmTargetRequiresRunningReplacementWhenBaselineRan(t *testing.T) {+ for _, test := range []struct {+ name string+ running bool+ status container.ContainerState+ confirmed bool+ }{+ {+ name: "running replacement",+ running: true,+ status: container.StateRunning,+ confirmed: true,+ },+ {+ name: "exited replacement",+ status: container.StateExited,+ },+ } {+ t.Run(test.name, func(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ switch {+ case strings.HasSuffix(r.URL.Path, "/containers/json"):+ assert.NoError(t, json.MarshalWrite(w, []container.Summary{{ID: "new", Names: []string{"/app"}}}))+ case strings.Contains(r.URL.Path, "/images/"):+ assert.NoError(t, json.MarshalWrite(w, image.InspectResponse{ID: "sha256:desired"}))+ case strings.Contains(r.URL.Path, "/containers/"):+ assert.NoError(t, json.MarshalWrite(w, container.InspectResponse{+ ID: "new",+ Name: "/app",+ Image: "sha256:desired",+ Config: &container.Config{},+ State: &container.State{+ Status: test.status,+ Running: test.running,+ },+ }))+ default:+ http.NotFound(w, r)+ }+ }))+ defer server.Close()+ svc := newTestService(t, server, nil)+ confirmed, unchanged, err := svc.ConfirmTarget(t.Context(), arcaneupdater.FrozenUpdateTarget{+ ContainerID: "old",+ ContainerName: "app",+ BaselineImageID: "sha256:baseline",+ BaselineStartedAt: "baseline",+ BaselineRunning: true,+ DesiredImageRef: "registry.example.com/app:latest",+ DesiredDigest: "sha256:desired",+ })+ require.NoError(t, err)+ require.Equal(t, test.confirmed, confirmed)+ require.False(t, unchanged)+ })+ }+}++func TestConfirmTargetStartsCreatedReplacementWhenBaselineRan(t *testing.T) {+ var started atomic.Bool+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ switch {+ case strings.HasSuffix(r.URL.Path, "/containers/json"):+ assert.NoError(t, json.MarshalWrite(w, []container.Summary{{ID: "new", Names: []string{"/app"}}}))+ case strings.HasSuffix(r.URL.Path, "/containers/new/start"):+ started.Store(true)+ w.WriteHeader(http.StatusNoContent)+ case strings.Contains(r.URL.Path, "/images/"):+ assert.NoError(t, json.MarshalWrite(w, image.InspectResponse{ID: "sha256:desired"}))+ case strings.Contains(r.URL.Path, "/containers/"):+ assert.NoError(t, json.MarshalWrite(w, container.InspectResponse{+ ID: "new",+ Name: "/app",+ Image: "sha256:desired",+ Config: &container.Config{},+ State: &container.State{Status: container.StateCreated},+ }))+ default:+ http.NotFound(w, r)+ }+ }))+ defer server.Close()+ svc := newTestService(t, server, nil)++ confirmed, unchanged, err := svc.ConfirmTarget(t.Context(), arcaneupdater.FrozenUpdateTarget{+ ContainerID: "old",+ ContainerName: "app",+ BaselineImageID: "sha256:baseline",+ BaselineStartedAt: "baseline",+ BaselineRunning: true,+ DesiredImageRef: "registry.example.com/app:latest",+ DesiredDigest: "sha256:desired",+ })+ require.NoError(t, err)+ require.True(t, started.Load())+ require.True(t, confirmed)+ require.False(t, unchanged)+}++func TestResumePlanStartsStoppedOriginalAndPersistsNewBaseline(t *testing.T) {+ target := arcaneupdater.FrozenUpdateTarget{+ ContainerID: "old",+ ContainerName: "app",+ BaselineImageID: "sha256:baseline",+ BaselineStartedAt: "baseline",+ BaselineRunning: true,+ DesiredImageRef: "registry.example.com/app:latest",+ DesiredDigest: "sha256:desired",+ }+ raw, err := json.Marshal(frozenUpdatePlan{+ Records: []updater.ImageUpdateRecord{{+ ContainerID: "old",+ ID: "image",+ Repository: "registry.example.com/app",+ Tag: "latest",+ HasUpdate: true,+ }},+ Targets: []arcaneupdater.FrozenUpdateTarget{target},+ })+ require.NoError(t, err)+ run := scheduler.Run{ID: "run", Outcome: scheduler.Outcome{Targets: []scheduler.TargetOutcome{{+ ID: "auto-update",+ ResourceType: "update-batch",+ Status: scheduler.Running,+ RecoveryData: raw,+ }}}}+ var started atomic.Bool+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ switch {+ case strings.HasSuffix(r.URL.Path, "/containers/json"):+ assert.NoError(t, json.MarshalWrite(w, []container.Summary{{ID: "old", Names: []string{"/app"}}}))+ case strings.HasSuffix(r.URL.Path, "/containers/old/start"):+ started.Store(true)+ w.WriteHeader(http.StatusNoContent)+ case strings.Contains(r.URL.Path, "/images/"):+ assert.NoError(t, json.MarshalWrite(w, image.InspectResponse{ID: "sha256:desired"}))+ case strings.Contains(r.URL.Path, "/containers/"):+ assert.NoError(t, json.MarshalWrite(w, container.InspectResponse{+ ID: "old",+ Name: "/app",+ Image: "sha256:baseline",+ Config: &container.Config{},+ State: &container.State{+ Running: started.Load(),+ StartedAt: kit.Ternary(started.Load(), "restarted", "baseline"),+ },+ }))+ default:+ http.NotFound(w, r)+ }+ }))+ defer server.Close()+ svc := newTestService(t, server, nil)+ var persisted []byte+ ctx := jobcontext.WithExecution(t.Context(), run, func(progress scheduler.TargetOutcome) error {+ if progress.ID == "auto-update" {+ persisted = progress.RecoveryData+ }+ return nil+ })++ resumed, remaining, unresolved, err := svc.ResumePlan(ctx, run)+ require.NoError(t, err)+ require.True(t, started.Load(), "the stopped original is started before the update resumes")+ require.False(t, unresolved)+ require.Equal(t, 1, remaining)+ plan := resumed.Value(frozenPendingKey{}).(*frozenUpdatePlan)+ require.Equal(t, "restarted", plan.Targets[0].BaselineStartedAt)+ var saved frozenUpdatePlan+ require.NoError(t, json.Unmarshal(persisted, &saved))+ require.Equal(t, "restarted", saved.Targets[0].BaselineStartedAt)+}diff --git a/backend/internal/updater/handler.go b/backend/internal/updater/handler.goindex a3f1e4dc5..2af599103 100644--- a/backend/internal/updater/handler.go+++ b/backend/internal/updater/handler.go@@ -99,7 +99,7 @@ func (h *UpdaterHandler) updateContainer(ctx context.Context, input *UpdateContaif input.Async {acceptedActivity, err := h.updaterService.AcceptSingleContainerUpdate(runtimeCtx, input.ContainerID)if err != nil {- return nil, huma.Error500InternalServerError("Failed to accept container update: " + err.Error())+ return nil, huma.NewError(common.ToAPIError(err).HTTPStatus(), "Failed to accept container update: "+err.Error())}return &updateContainerOutput{Status: http.StatusAccepted,@@ -111,7 +111,7 @@ func (h *UpdaterHandler) updateContainer(ctx context.Context, input *UpdateConta}out, err := h.updaterService.UpdateSingleContainer(runtimeCtx, input.ContainerID)if err != nil {- return nil, huma.Error500InternalServerError("Failed to run updater: " + err.Error())+ return nil, huma.NewError(common.ToAPIError(err).HTTPStatus(), "Failed to run updater: "+err.Error())}return &updateContainerOutput{diff --git a/backend/internal/updater/service.go b/backend/internal/updater/service.goindex 0ced7dc1b..221805780 100644--- a/backend/internal/updater/service.go+++ b/backend/internal/updater/service.go@@ -64,7 +64,7 @@ import ()var (- errUpdateBusy = errors.New("another container update is running")+ errUpdateBusy = common.Classify(common.ErrConflict, errors.New("another container update is running"))containerEventTypes = map[string]event.EventType{"container_stop": event.EventTypeContainerStop,@@ -199,6 +199,7 @@ func NewUpdaterService(RecordUpdate: service.recordSingleContainerUpdate,FreezeContainer: service.recovery.FreezeContainer,ConfirmTarget: service.recovery.ConfirmTarget,+ ResumeTarget: service.recovery.ResumeTarget,WithFrozenTarget: service.recovery.WithSingleTarget,})// The self container ID routes Arcane through the CLI self-updater even without its labels.@@ -1291,6 +1292,19 @@ func (s *UpdaterService) acquireUpdate(ctx context.Context) (context.Context, fureturn context.WithValue(ctx, updateAdmissionKey{}, s), func() { lease.Release(ctx) }, nil}+func (s *UpdaterService) PruneHistory(ctx context.Context, retentionDays int) (int64, error) {+ if retentionDays <= 0 {+ return 0, nil+ }+ result := s.deps.DB.WithContext(ctx).+ Where("start_time < ?", time.Now().Add(-time.Duration(retentionDays)*24*time.Hour)).+ Delete(&AutoUpdateRecord{})+ if result.Error != nil {+ return 0, fmt.Errorf("failed to prune auto-update history: %w", result.Error)+ }+ return result.RowsAffected, nil+}+// RecordUpdateRun persists one updater resource result into Arcane history.func (s *UpdaterService) RecordUpdateRun(ctx context.Context, result updater.ResourceResult) error {now := time.Now()diff --git a/backend/pkg/libarcane/activity/handler.go b/backend/pkg/libarcane/activity/handler.goindex 1eb70bd27..8ef28d459 100644--- a/backend/pkg/libarcane/activity/handler.go+++ b/backend/pkg/libarcane/activity/handler.go@@ -221,6 +221,17 @@ func WriteDoneLine(writer io.Writer) {}}+func WriteErrorLine(writer io.Writer, err error) {+ if writer == nil || err == nil {+ return+ }+ _ = json.MarshalWrite(writer, map[string]string{"error": err.Error()})+ _, _ = io.WriteString(writer, "\n")+ if flusher, ok := writer.(http.Flusher); ok {+ flusher.Flush()+ }+}+func WriteStartedLine(writer io.Writer, activityID string) {if writer == nil || strings.TrimSpace(activityID) == "" {returndiff --git a/backend/pkg/libarcane/activity/handler_test.go b/backend/pkg/libarcane/activity/handler_test.gonew file mode 100644index 000000000..964fd443b--- /dev/null+++ b/backend/pkg/libarcane/activity/handler_test.go@@ -0,0 +1,20 @@+package activity++import (+ "bytes"+ "encoding/json/v2"+ "errors"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestWriteErrorLineEncodesControlBytes(t *testing.T) {+ var buf bytes.Buffer+ WriteErrorLine(&buf, errors.New("pull failed \x1b[31mred\x1b[0m"))++ require.Equal(t, byte('\n'), buf.Bytes()[buf.Len()-1])+ var frame map[string]string+ require.NoError(t, json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &frame))+ require.Equal(t, "pull failed \x1b[31mred\x1b[0m", frame["error"])+}diff --git a/backend/pkg/libarcane/activity/writer.go b/backend/pkg/libarcane/activity/writer.goindex 33925ba58..3040b6adb 100644--- a/backend/pkg/libarcane/activity/writer.go+++ b/backend/pkg/libarcane/activity/writer.go@@ -9,6 +9,8 @@ import ("net/http""strings""sync"+ "sync/atomic"+ "time"activitytypes "github.com/getarcaneapp/arcane/types/v2/activity""github.com/samber/mo"@@ -33,6 +35,9 @@ type Writer struct {mu sync.Mutexbuffer []byte++ dropped atomic.Uint64+ lastDropWarn atomic.Int64}const (@@ -40,6 +45,8 @@ const (// writerAppendBatchSize caps how many queued lines are drained into one// AppendMessages transaction.writerAppendBatchSize = 32++ writerDropWarningInterval = 5 * time.Second)type writerAppendMessage struct {@@ -111,13 +118,23 @@ func (w *Writer) Flush() {if flusher, ok := w.writer.(http.Flusher); ok {flusher.Flush()}+ w.mu.Lock()+ residual := w.processLineInternal(strings.TrimSpace(string(w.buffer)))+ w.buffer = nil+ w.mu.Unlock()+ if message, ok := residual.Get(); ok {+ select {+ case w.queueCh <- writerQueueItem{message: &message}:+ case <-doneInternal(w.ctx):+ return+ }+ }+flushDone := make(chan struct{})select {case w.queueCh <- writerQueueItem{flush: flushDone}:case <-doneInternal(w.ctx):return- default:- return}select {case <-flushDone:@@ -176,9 +193,13 @@ func (w *Writer) enqueueMessageInternal(message writerAppendMessage) {select {case w.queueCh <- writerQueueItem{message: &message}:case <-doneInternal(w.ctx):- returndefault:- return+ w.dropped.Add(1)+ now := time.Now().UnixNano()+ lastWarned := w.lastDropWarn.Load()+ if now-lastWarned >= writerDropWarningInterval.Nanoseconds() && w.lastDropWarn.CompareAndSwap(lastWarned, now) {+ slog.WarnContext(w.persistCtx, "activity output queue full; dropping lines", "activityId", w.activityID, "droppedCount", w.dropped.Swap(0))+ }}}diff --git a/backend/pkg/libarcane/activity/writer_test.go b/backend/pkg/libarcane/activity/writer_test.goindex bcc4001e0..b4477c4fd 100644--- a/backend/pkg/libarcane/activity/writer_test.go+++ b/backend/pkg/libarcane/activity/writer_test.go@@ -68,19 +68,20 @@ func TestWriterRecordsLogAndErrorFramesVerbatimInternal(t *testing.T) {appender := &recordingAppender{}writer := NewWriter(t.Context(), appender, "activity-1", io.Discard, "Deploying project")- _, err := writer.Write([]byte("{\"log\":\"Container web-1 Created\"}\n{\"error\":\"Error response from daemon: conflict\"}\n"))+ _, err := writer.Write([]byte("{\"log\":\"Container web-1 Created\"}\n{\"error\":\"Error response from daemon: conflict\"}\nno trailing newline"))require.NoError(t, err)FlushWriter(writer)require.Eventually(t, func() bool {messages, _ := appender.recorded()- return len(messages) == 2+ return len(messages) == 3}, time.Second, 10*time.Millisecond)messages, _ := appender.recorded()require.Equal(t, "Container web-1 Created", messages[0].Message)require.Equal(t, activitytypes.MessageLevelInfo, messages[0].Level)require.Equal(t, "Error response from daemon: conflict", messages[1].Message)require.Equal(t, activitytypes.MessageLevelError, messages[1].Level)+ require.Equal(t, "no trailing newline", messages[2].Message)}// gatedAppender blocks its first AppendMessages call until release isdiff --git a/backend/pkg/libarcane/system/gpu.go b/backend/pkg/libarcane/system/gpu.goindex 79caeb504..227bf48b6 100644--- a/backend/pkg/libarcane/system/gpu.go+++ b/backend/pkg/libarcane/system/gpu.go@@ -17,6 +17,8 @@ import (systemtypes "github.com/getarcaneapp/arcane/types/v2/system""github.com/samber/hot"+ kit "go.getarcane.app/kit/pkg"+ "golang.org/x/sync/singleflight")// AMDGPUSysfsPath is the sysfs base used to discover AMD GPUs.@@ -46,6 +48,9 @@ type GPUMonitor struct {// gpuStatsTTL so concurrent consumers and fast sampler ticks share one// process fork instead of each launching their own.statsCache atomic.Pointer[gpuStatsSnapshotInternal]+ statsGroup singleflight.Group++ announcedType stringenabled bool}@@ -91,8 +96,19 @@ func (m *GPUMonitor) Stats(ctx context.Context) ([]systemtypes.GPUStats, error)return snapshot.stats, snapshot.err}- stats, err := m.statsUncachedInternal(ctx)- m.statsCache.Store(&gpuStatsSnapshotInternal{stats: stats, err: err, sampledAt: time.Now()})+ result, err, _ := m.statsGroup.Do("stats", func() (any, error) {+ previous := m.statsCache.Load()+ if previous != nil && time.Since(previous.sampledAt) < gpuStatsTTL {+ return previous.stats, previous.err+ }+ stats, statsErr := m.statsUncachedInternal(ctx)+ if previousErr := kit.FromPtr(previous).err; statsErr != nil && (previousErr == nil || previousErr.Error() != statsErr.Error()) {+ slog.WarnContext(ctx, "GPU stats unavailable", "error", statsErr)+ }+ m.statsCache.Store(&gpuStatsSnapshotInternal{stats: stats, err: statsErr, sampledAt: time.Now()})+ return stats, statsErr+ })+ stats, _ := result.([]systemtypes.GPUStats)return stats, err}@@ -135,9 +151,12 @@ func (m *GPUMonitor) statsForTypeInternal(ctx context.Context, gpuType string) (}// markDetected records a successful detection.-func (m *GPUMonitor) markDetectedInternal(gpuType, toolPath string) {+func (m *GPUMonitor) markDetectedInternal(gpuType, toolPath string) bool {m.detectionCache.Set(struct{}{}, gpuDetection{detected: true, gpuType: gpuType, toolPath: toolPath})m.detectionDone = true+ announce := m.announcedType != gpuType+ m.announcedType = gpuType+ return announce}// detect runs vendor probing under detectionMu. The configuredType pin is honored when set@@ -150,22 +169,25 @@ func (m *GPUMonitor) detectInternal(ctx context.Context) error {switch t {case "nvidia":if path, err := exec.LookPath("nvidia-smi"); err == nil {- m.markDetectedInternal("nvidia", path)- slog.InfoContext(ctx, "Using configured GPU type", "type", "nvidia")+ if m.markDetectedInternal("nvidia", path) {+ slog.InfoContext(ctx, "Using configured GPU type", "type", "nvidia")+ }return nil}return errors.New("nvidia-smi not found but GPU_TYPE set to nvidia")case "amd":if HasAMDGPU() {- m.markDetectedInternal("amd", AMDGPUSysfsPath)- slog.InfoContext(ctx, "Using configured GPU type", "type", "amd")+ if m.markDetectedInternal("amd", AMDGPUSysfsPath) {+ slog.InfoContext(ctx, "Using configured GPU type", "type", "amd")+ }return nil}return errors.New("AMD GPU not found in sysfs but GPU_TYPE set to amd")case "intel":if path, err := exec.LookPath("intel_gpu_top"); err == nil {- m.markDetectedInternal("intel", path)- slog.InfoContext(ctx, "Using configured GPU type", "type", "intel")+ if m.markDetectedInternal("intel", path) {+ slog.InfoContext(ctx, "Using configured GPU type", "type", "intel")+ }return nil}return errors.New("intel_gpu_top not found but GPU_TYPE set to intel")@@ -175,18 +197,21 @@ func (m *GPUMonitor) detectInternal(ctx context.Context) error {}if path, err := exec.LookPath("nvidia-smi"); err == nil {- m.markDetectedInternal("nvidia", path)- slog.InfoContext(ctx, "NVIDIA GPU detected", "tool", "nvidia-smi", "path", path)+ if m.markDetectedInternal("nvidia", path) {+ slog.InfoContext(ctx, "NVIDIA GPU detected", "tool", "nvidia-smi", "path", path)+ }return nil}if HasAMDGPU() {- m.markDetectedInternal("amd", AMDGPUSysfsPath)- slog.InfoContext(ctx, "AMD GPU detected", "method", "sysfs", "path", AMDGPUSysfsPath)+ if m.markDetectedInternal("amd", AMDGPUSysfsPath) {+ slog.InfoContext(ctx, "AMD GPU detected", "method", "sysfs", "path", AMDGPUSysfsPath)+ }return nil}if path, err := exec.LookPath("intel_gpu_top"); err == nil {- m.markDetectedInternal("intel", path)- slog.InfoContext(ctx, "Intel GPU detected", "tool", "intel_gpu_top", "path", path)+ if m.markDetectedInternal("intel", path) {+ slog.InfoContext(ctx, "Intel GPU detected", "tool", "intel_gpu_top", "path", path)+ }return nil}@@ -233,7 +258,6 @@ func getNvidiaStatsInternal(ctx context.Context) ([]systemtypes.GPUStats, error)output, err := cmd.Output()if err != nil {- slog.WarnContext(ctx, "Failed to execute nvidia-smi", "error", err)return nil, fmt.Errorf("nvidia-smi execution failed: %w", err)}@@ -241,7 +265,6 @@ func getNvidiaStatsInternal(ctx context.Context) ([]systemtypes.GPUStats, error)reader.TrimLeadingSpace = truerecords, err := reader.ReadAll()if err != nil {- slog.WarnContext(ctx, "Failed to parse nvidia-smi CSV output", "error", err)return nil, fmt.Errorf("failed to parse nvidia-smi output: %w", err)}diff --git a/backend/pkg/libarcane/ws/benchmark_test.go b/backend/pkg/libarcane/ws/benchmark_test.goindex 029ccf1e6..3576b5fa0 100644--- a/backend/pkg/libarcane/ws/benchmark_test.go+++ b/backend/pkg/libarcane/ws/benchmark_test.go@@ -76,7 +76,7 @@ func benchHub(b *testing.B, numClients, hubBuf int) (*Hub, func()) {}// Wait for all clients to register- for h.ClientCount() < numClients {+ for h.Len() < numClients {time.Sleep(time.Millisecond)}diff --git a/backend/pkg/libarcane/ws/bridge.go b/backend/pkg/libarcane/ws/bridge.goindex 473d2d1b2..e6994234b 100644--- a/backend/pkg/libarcane/ws/bridge.go+++ b/backend/pkg/libarcane/ws/bridge.go@@ -27,7 +27,7 @@ func ForwardLines(ctx context.Context, hub *Hub, lines <-chan string) {if !ok {return}- hub.Broadcast([]byte(line))+ hub.Publish(ctx, []byte(line))}}}@@ -48,7 +48,7 @@ func ForwardLogJSON(ctx context.Context, hub *Hub, logs <-chan LogMessage) {m.Timestamp = NowRFC3339()}if b, err := json.Marshal(m); err == nil {- hub.Broadcast(b)+ hub.Publish(ctx, b)}}}@@ -75,7 +75,7 @@ func ForwardLogJSONBatched(ctx context.Context, hub *Hub, logs <-chan LogMessage}b, err := json.Marshal(buf)if err == nil {- hub.Broadcast(b)+ hub.Publish(ctx, b)}buf = buf[:0]}diff --git a/backend/pkg/libarcane/ws/bridge_test.go b/backend/pkg/libarcane/ws/bridge_test.goindex 7ed32df8c..540393819 100644--- a/backend/pkg/libarcane/ws/bridge_test.go+++ b/backend/pkg/libarcane/ws/bridge_test.go@@ -21,7 +21,7 @@ func registerCollectorInternal(t *testing.T, h *Hub) *Client {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() >= 1+ return h.Len() >= 1}, time.Second, 5*time.Millisecond)return cdiff --git a/backend/pkg/libarcane/ws/client_test.go b/backend/pkg/libarcane/ws/client_test.goindex 6124ccbc1..d0986fe03 100644--- a/backend/pkg/libarcane/ws/client_test.go+++ b/backend/pkg/libarcane/ws/client_test.go@@ -54,7 +54,7 @@ func TestServeClient_ReceivesBroadcast(t *testing.T) {// Wait for registrationrequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Broadcast a message@@ -87,7 +87,7 @@ func TestServeClient_ClientDisconnect(t *testing.T) {require.NoError(t, err)require.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Close the client connection@@ -95,7 +95,7 @@ func TestServeClient_ClientDisconnect(t *testing.T) {// The hub should eventually remove the clientrequire.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, 5*time.Second, 50*time.Millisecond)}@@ -123,7 +123,7 @@ func TestServeClient_ContextCancellation(t *testing.T) {}()require.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Cancel the client context@@ -131,7 +131,7 @@ func TestServeClient_ContextCancellation(t *testing.T) {// Client should be removed from hubrequire.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, 5*time.Second, 50*time.Millisecond)}@@ -222,7 +222,7 @@ func TestServeClient_MultipleMessages(t *testing.T) {<-serverReadyrequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Send multiple messages and verify order@@ -277,5 +277,5 @@ func TestServeClient_StoppedHubDoesNotBlock(t *testing.T) {// The caller owns the connection and its bookkeeping on failure, so it can// retry the same conn against a fresh hub.assert.False(t, onRemoveCalled.Load(), "onRemove must not run when registration fails")- assert.Equal(t, 0, h.ClientCount())+ assert.Equal(t, 0, h.Len())}diff --git a/backend/pkg/libarcane/ws/goroutine_leak_test.go b/backend/pkg/libarcane/ws/goroutine_leak_test.goindex 9dd8e8332..3d9d0046d 100644--- a/backend/pkg/libarcane/ws/goroutine_leak_test.go+++ b/backend/pkg/libarcane/ws/goroutine_leak_test.go@@ -146,7 +146,7 @@ func TestLeak_ServeClientFullLifecycle(t *testing.T) {require.NoError(t, err)require.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// The client is connected; now we should have extra goroutines:@@ -158,7 +158,7 @@ func TestLeak_ServeClientFullLifecycle(t *testing.T) {_ = clientConn.CloseNow()require.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, 3*time.Second, 50*time.Millisecond, "client should be removed from hub after close")// Cancel context to stop Hub.Run (simulates OnEmpty calling cancel())@@ -197,7 +197,7 @@ func TestLeak_OnEmptyCallbackCancelsContext(t *testing.T) {require.NoError(t, err)require.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Close client -> triggers safeRemove -> hub.remove -> OnEmpty -> cancel()@@ -593,7 +593,7 @@ func TestLeak_MultipleClientsOnSameHub(t *testing.T) {}require.Eventually(t, func() bool {- return h.ClientCount() == numClients+ return h.Len() == numClients}, 2*time.Second, 10*time.Millisecond)// Close all clientsdiff --git a/backend/pkg/libarcane/ws/hub.go b/backend/pkg/libarcane/ws/hub.goindex bcc1c2e43..294357863 100644--- a/backend/pkg/libarcane/ws/hub.go+++ b/backend/pkg/libarcane/ws/hub.go@@ -110,6 +110,20 @@ func (h *Hub) Run(ctx context.Context) {}}+func (h *Hub) Len() int {+ h.mu.RLock()+ defer h.mu.RUnlock()+ return len(h.clients)+}++func (h *Hub) Publish(ctx context.Context, msg []byte) {+ select {+ case h.broadcast <- msg:+ case <-h.stopped:+ case <-ctx.Done():+ }+}+func (h *Hub) Broadcast(msg []byte) {select {case h.broadcast <- msg:diff --git a/backend/pkg/libarcane/ws/hub_test.go b/backend/pkg/libarcane/ws/hub_test.goindex 3edbd612a..fdc29cc52 100644--- a/backend/pkg/libarcane/ws/hub_test.go+++ b/backend/pkg/libarcane/ws/hub_test.go@@ -45,16 +45,10 @@ func newTestWSPairInternal(t *testing.T) (serverConn *websocket.Conn, cleanup fu}}-func (h *Hub) ClientCount() int {- h.mu.RLock()- defer h.mu.RUnlock()- return len(h.clients)-}-func TestNewHub(t *testing.T) {h := NewHub(10)require.NotNil(t, h)- assert.Equal(t, 0, h.ClientCount())+ assert.Equal(t, 0, h.Len())}func TestHub_RegisterAndClientCount(t *testing.T) {@@ -70,7 +64,7 @@ func TestHub_RegisterAndClientCount(t *testing.T) {// Wait for registration to processrequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)}@@ -86,13 +80,13 @@ func TestHub_UnregisterClient(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)h.unregister <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, time.Second, 5*time.Millisecond)}@@ -108,7 +102,7 @@ func TestHub_Broadcast(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)msg := []byte("hello world")@@ -145,7 +139,7 @@ func TestHub_BroadcastToMultipleClients(t *testing.T) {}()require.Eventually(t, func() bool {- return h.ClientCount() == numClients+ return h.Len() == numClients}, time.Second, 5*time.Millisecond)msg := []byte("broadcast to all")@@ -174,7 +168,7 @@ func TestHub_BackpressureDropsSlowClient(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Fill the client's send buffer@@ -184,7 +178,7 @@ func TestHub_BackpressureDropsSlowClient(t *testing.T) {h.Broadcast([]byte("overflow"))require.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, time.Second, 5*time.Millisecond)}@@ -205,7 +199,7 @@ func TestHub_OnEmptyCallback(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)h.unregister <- c@@ -232,7 +226,7 @@ func TestHub_OnFirstClientCallback(t *testing.T) {h.register <- NewClient(sc2, 16)require.Eventually(t, func() bool {- return h.ClientCount() == 2+ return h.Len() == 2}, time.Second, 5*time.Millisecond)require.Eventually(t, func() bool {return callCount.Load() == 1@@ -261,13 +255,13 @@ func TestHub_OnEmptyCalledOnlyOnce(t *testing.T) {h.register <- c2require.Eventually(t, func() bool {- return h.ClientCount() == 2+ return h.Len() == 2}, time.Second, 5*time.Millisecond)// Unregister first — should NOT trigger onEmptyh.unregister <- c1require.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)time.Sleep(50 * time.Millisecond)@@ -297,7 +291,7 @@ func TestHub_ContextCancellation(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)cancel()@@ -310,7 +304,7 @@ func TestHub_ContextCancellation(t *testing.T) {}// After closeAll, clients map should be empty- assert.Equal(t, 0, h.ClientCount())+ assert.Equal(t, 0, h.Len())}func TestHub_BroadcastBufferFull(t *testing.T) {@@ -401,7 +395,7 @@ func TestHub_ConcurrentOperations(t *testing.T) {// All clients should be gonerequire.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, 2*time.Second, 10*time.Millisecond)}@@ -417,18 +411,18 @@ func TestHub_DoubleUnregister(t *testing.T) {h.register <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 1+ return h.Len() == 1}, time.Second, 5*time.Millisecond)// Unregister twice — should not panich.unregister <- crequire.Eventually(t, func() bool {- return h.ClientCount() == 0+ return h.Len() == 0}, time.Second, 5*time.Millisecond)// Second unregister should be harmlessh.unregister <- ctime.Sleep(50 * time.Millisecond)- assert.Equal(t, 0, h.ClientCount())+ assert.Equal(t, 0, h.Len())}diff --git a/backend/pkg/libarcane/ws/proxy.go b/backend/pkg/libarcane/ws/proxy.goindex 67ff10aaa..044361e3f 100644--- a/backend/pkg/libarcane/ws/proxy.go+++ b/backend/pkg/libarcane/ws/proxy.go@@ -87,6 +87,7 @@ func ProxyHTTP(w http.ResponseWriter, r *http.Request, remoteWS string, header hgo pump(clientConn, remoteConn)go pump(remoteConn, clientConn)go KeepAlive(pumpCtx, pumpCancel, clientConn)+ go KeepAlive(pumpCtx, pumpCancel, remoteConn)<-errcreturn nildiff --git a/backend/pkg/projects/cache.go b/backend/pkg/projects/cache.goindex 812a7cf0f..6b8396133 100644--- a/backend/pkg/projects/cache.go+++ b/backend/pkg/projects/cache.go@@ -20,10 +20,15 @@ type composeCacheInternal[T any] struct {type composeCacheEntryInternal[T any] struct {fingerprint string- mtimes map[string]time.Time+ stamps map[string]composeCacheStampInternalvalue T}+type composeCacheStampInternal struct {+ modTime time.Time+ size int64+}+// NewComposeCache caches Compose-derived values and their file dependencies.// clone, when supplied, isolates stored values from mutations by callers.func NewComposeCache[T any](capacity int, clone func(T) T) projecttypes.ComposeCache[T] {@@ -45,7 +50,7 @@ func cloneComposeProjectInternal(project *composetypes.Project) *composetypes.Prfunc (c *composeCacheInternal[T]) Get(projectID, fingerprint string) (T, bool) {entry, found := c.entries.Peek(projectID)- if found && entry.fingerprint == fingerprint && validComposeCacheEntryInternal(entry.mtimes) {+ if found && entry.fingerprint == fingerprint && validComposeCacheEntryInternal(entry.stamps) {if c.clone != nil {return c.clone(entry.value), true}@@ -55,11 +60,11 @@ func (c *composeCacheInternal[T]) Get(projectID, fingerprint string) (T, bool) {return zero, false}-func (c *composeCacheInternal[T]) Set(projectID, fingerprint, projectPath, projectsDirectory, composePath string, composeFiles, envFiles []string, value T) error {+func (c *composeCacheInternal[T]) Set(projectID, fingerprint, projectPath, projectsDirectory, composePath string, composeFiles, envFiles []string, loadedAt time.Time, value T) error {if strings.TrimSpace(projectID) == "" {return nil}- mtimes := make(map[string]time.Time)+ stamps := make(map[string]composeCacheStampInternal)for _, path := range append([]string{composePath}, composeFiles...) {if path == "" {continue@@ -68,7 +73,10 @@ func (c *composeCacheInternal[T]) Set(projectID, fingerprint, projectPath, projeif err != nil {return fmt.Errorf("stat compose file %s: %w", path, err)}- mtimes[path] = info.ModTime()+ if !info.ModTime().Before(loadedAt) {+ return nil+ }+ stamps[path] = composeCacheStampInternal{modTime: info.ModTime(), size: info.Size()}}dependencies := append([]string{filepath.Join(projectPath, EffectiveEnvFileName)}, envFiles...)if projectsDirectory != "" {@@ -81,18 +89,21 @@ func (c *composeCacheInternal[T]) Set(projectID, fingerprint, projectPath, projefor _, path := range dependencies {info, err := os.Stat(path)if err == nil {- mtimes[path] = info.ModTime()+ if !info.ModTime().Before(loadedAt) {+ return nil+ }+ stamps[path] = composeCacheStampInternal{modTime: info.ModTime(), size: info.Size()}continue}if !os.IsNotExist(err) {return fmt.Errorf("stat compose dependency %s: %w", path, err)}- mtimes[path] = time.Time{}+ stamps[path] = composeCacheStampInternal{}}if c.clone != nil {value = c.clone(value)}- c.entries.Set(projectID, composeCacheEntryInternal[T]{fingerprint: fingerprint, mtimes: mtimes, value: value})+ c.entries.Set(projectID, composeCacheEntryInternal[T]{fingerprint: fingerprint, stamps: stamps, value: value})return nil}@@ -100,16 +111,16 @@ func (c *composeCacheInternal[T]) Invalidate(projectID string) {c.entries.Delete(projectID)}-func validComposeCacheEntryInternal(mtimes map[string]time.Time) bool {- for path, mtime := range mtimes {+func validComposeCacheEntryInternal(stamps map[string]composeCacheStampInternal) bool {+ for path, stamp := range stamps {info, err := os.Stat(path)if err != nil {- if !os.IsNotExist(err) || !mtime.IsZero() {+ if !os.IsNotExist(err) || !stamp.modTime.IsZero() {return false}continue}- if mtime.IsZero() || !info.ModTime().Equal(mtime) {+ if stamp.modTime.IsZero() || !info.ModTime().Equal(stamp.modTime) || info.Size() != stamp.size {return false}// A chmod does not bump mtime, so confirm the file is still readable.@@ -143,6 +154,7 @@ func LoadCachedComposeProject(ctx context.Context,}}dependencies := projecttypes.ComposeDependencies{}+ loadedAt := time.Now()model, err := LoadComposeProject(ctx, composePath, projectName, projectsDirectory, autoInject, pathMapper, nil, nil, false, &dependencies, nil, nil)if err != nil {return nil, err@@ -155,7 +167,7 @@ func LoadCachedComposeProject(ctx context.Context,// Metadata discovery failure disables caching; the executable model is valid.return model, nil}- if setErr := cache.Set(projectID, fingerprint, projectPath, projectsDirectory, composePath, composeFiles, envFiles, model); setErr != nil {+ if setErr := cache.Set(projectID, fingerprint, projectPath, projectsDirectory, composePath, composeFiles, envFiles, loadedAt, model); setErr != nil {return nil, setErr}return model, nildiff --git a/backend/pkg/projects/compose.go b/backend/pkg/projects/compose.goindex 9425c431c..14f94988f 100644--- a/backend/pkg/projects/compose.go+++ b/backend/pkg/projects/compose.go@@ -138,8 +138,9 @@ var plainComposeClient atomic.Pointer[plainComposeClientEntryInternal]// dockerHost; empty resolves the host from the process environment.func plainComposeClientInternal(dockerHost string) (*Client, error) {host := cmp.Or(dockerHost, os.Getenv("DOCKER_HOST"))- if entry := plainComposeClient.Load(); entry != nil && entry.host == host {- return entry.client, nil+ current := plainComposeClient.Load()+ if current != nil && current.host == host {+ return current.client, nil}// Built on context.Background so request-scoped values (progress@@ -148,9 +149,13 @@ func plainComposeClientInternal(dockerHost string) (*Client, error) {if err != nil {return nil, err}- // Concurrent first calls may race and briefly duplicate a client; the- // last store wins and later calls converge on it.- plainComposeClient.Store(&plainComposeClientEntryInternal{host: host, client: c})+ if !plainComposeClient.CompareAndSwap(current, &plainComposeClientEntryInternal{host: host, client: c}) {+ _ = c.Close()+ return plainComposeClientInternal(dockerHost)+ }+ if current != nil {+ _ = current.client.Close()+ }return c, nil}diff --git a/backend/pkg/projects/coordinator.go b/backend/pkg/projects/coordinator.goindex 0325ad40d..a5084bd87 100644--- a/backend/pkg/projects/coordinator.go+++ b/backend/pkg/projects/coordinator.go@@ -40,7 +40,7 @@ func NewCoordinator(commands projecttypes.ComposeCommands) projecttypes.ComposeCfunc (c *composeCoordinatorInternal) Deploy(ctx context.Context, request projecttypes.ComposeDeployment) (_ *composetypes.Project, err error) {defer func() {if err != nil && request.Recover != nil {- request.Recover(ctx)+ request.Recover(context.WithoutCancel(ctx))}}()if request.PreDeploy != nil {@@ -73,7 +73,7 @@ func (c *composeCoordinatorInternal) Deploy(ctx context.Context, request projectslog.InfoContext(ctx, "starting compose up with health check support", "projectId", request.ProjectID, "projectName", model.Name, "services", len(model.Services), "removeOrphans", removeOrphans)if upErr := c.commands.Up(ctx, model, nil, removeOrphans, forceRecreate, recreateVolumes, request.AuthConfigs, request.WaitTimeout); upErr != nil {slog.ErrorContext(ctx, "compose up failed", "projectName", model.Name, "projectId", request.ProjectID, "error", upErr)- if strings.Contains(upErr.Error(), "timeout") || strings.Contains(upErr.Error(), "context deadline exceeded") {+ if errors.Is(upErr, context.DeadlineExceeded) || strings.Contains(upErr.Error(), "application not healthy after") {return nil,fmt.Errorf("deployment timed out waiting for services - long-running 'service_healthy'/'service_completed_successfully' dependencies "+"may need a higher Deploy Wait Timeout setting, and 'service_healthy' requires a healthcheck: %w",@@ -96,13 +96,13 @@ func (c *composeCoordinatorInternal) UpdateServices(ctx context.Context, requestselected, err := SelectServices(request.Project, slices.Concat(scope, request.StoppedDependents))if err != nil {if request.RestoreBeforeMutation != nil {- request.RestoreBeforeMutation(ctx)+ request.RestoreBeforeMutation(context.WithoutCancel(ctx))}return err}if pullServicesErr := c.PullServices(ctx, selected, request.Services, request.Images, request.Progress); pullServicesErr != nil {if request.RestoreBeforeMutation != nil {- request.RestoreBeforeMutation(ctx)+ request.RestoreBeforeMutation(context.WithoutCancel(ctx))}return fmt.Errorf("pull updated service images: %w", pullServicesErr)}@@ -111,7 +111,7 @@ func (c *composeCoordinatorInternal) UpdateServices(ctx context.Context, request}if upErr := c.commands.Up(ctx, selected, scope, false, true, false, request.AuthConfigs, request.WaitTimeout); upErr != nil {if request.Recover != nil {- request.Recover(ctx)+ request.Recover(context.WithoutCancel(ctx))}return fmt.Errorf("failed to up services: %w", upErr)}@@ -120,7 +120,7 @@ func (c *composeCoordinatorInternal) UpdateServices(ctx context.Context, requestif len(request.StoppedDependents) > 0 {if createErr := c.commands.Create(ctx, selected, request.StoppedDependents, request.AuthConfigs); createErr != nil {if request.Recover != nil {- request.Recover(ctx)+ request.Recover(context.WithoutCancel(ctx))}return fmt.Errorf("failed to recreate stopped dependents: %w", createErr)}@@ -272,10 +272,15 @@ func (c *composeCoordinatorInternal) PrepareImagesForDeploy(}decision := DecideDeployImageAction(localSvc, pullPolicyOverride)- if updated {- decision = DeployImageDecision{Build: true}+ serviceDecision := decision+ if updated && !decision.Build {+ exists, existsErr := operations.Exists(ctx, imageName)+ if existsErr != nil {+ slog.WarnContext(ctx, "failed to check local image existence", "image", imageName, "error", existsErr)+ }+ serviceDecision = DeployImageDecision{Build: !exists}}- if err := c.ensureDeployServiceImageReadyInternal(ctx, projectID, project, name, localSvc, imageName, decision, progressWriter, operations); err != nil {+ if err := c.ensureDeployServiceImageReadyInternal(ctx, projectID, project, name, localSvc, imageName, serviceDecision, progressWriter, operations); err != nil {return err}diff --git a/backend/pkg/projects/discovery.go b/backend/pkg/projects/discovery.goindex 62faeb086..316a6fe9b 100644--- a/backend/pkg/projects/discovery.go+++ b/backend/pkg/projects/discovery.go@@ -86,7 +86,7 @@ func DiscoverProjectDirectories(ctx context.Context, root string, followSymlinksancestors := make(map[string]struct{})if err == nil {- err = walkProjectDirectoriesInternal(ctx, root, root, true, 0, maxDepth, followSymlinks, ancestors, &discovered)+ err = walkProjectDirectoriesInternal(ctx, root, root, true, 0, maxDepth, followSymlinks, projectScanSkipDirectorySetInternal(""), ancestors, &discovered)}if err != nil {// The process runs as the PUID/PGID runtime user (default 65532) even@@ -112,12 +112,13 @@ func walkProjectDirectoriesInternal(ctx context.Context,currentDepth,maxDepth int,followSymlinks bool,+ skipDirs map[string]bool,ancestors map[string]struct{},discovered *[]DiscoveredProjectDir,) error {if !isRoot {name := filepath.Base(path)- if IsIgnoredProjectDirName(name) {+ if IsIgnoredProjectDirName(name) || (currentDepth > 1 && skipDirs[name]) {return nil}}@@ -186,7 +187,18 @@ func walkProjectDirectoriesInternal(ctx context.Context,continue}- if walkProjectDirectoriesErr := walkProjectDirectoriesInternal(ctx, root, childPath, false, currentDepth+1, maxDepth, followSymlinks, ancestors, discovered); walkProjectDirectoriesErr != nil {+ if walkProjectDirectoriesErr := walkProjectDirectoriesInternal(+ ctx,+ root,+ childPath,+ false,+ currentDepth+1,+ maxDepth,+ followSymlinks,+ skipDirs,+ ancestors,+ discovered,+ ); walkProjectDirectoriesErr != nil {return walkProjectDirectoriesErr}}diff --git a/backend/pkg/projects/env.go b/backend/pkg/projects/env.goindex 1fdce5a89..a367788b8 100644--- a/backend/pkg/projects/env.go+++ b/backend/pkg/projects/env.go@@ -78,6 +78,7 @@ type EnvLoader struct {type envFileCacheEntry struct {path stringmtime time.Time+ size int64exists boolvalues EnvMap}@@ -230,13 +231,12 @@ func loadCachedEnvFileInternal(_ context.Context, envCache *hot.HotCache[string,}entry, found, err := envCache.GetWithLoaders(key, func(_ []string) (map[string]envFileCacheEntry, error) {- entry := envFileCacheEntry{path: path}// Stays on os.*: env files may be symlinks resolving outside any// confinement root (a supported setup), which acfs cannot follow.info, err := os.Stat(path)if err != nil {if errors.Is(err, os.ErrNotExist) {- return map[string]envFileCacheEntry{key: entry}, nil+ return map[string]envFileCacheEntry{key: {path: path}}, nil}return nil, err}@@ -248,10 +248,13 @@ func loadCachedEnvFileInternal(_ context.Context, envCache *hot.HotCache[string,if err != nil {return nil, fmt.Errorf("parse env file: %w", err)}- entry.exists = true- entry.mtime = info.ModTime()- entry.values = parsed- return map[string]envFileCacheEntry{key: entry}, nil+ return map[string]envFileCacheEntry{key: {+ path: path,+ mtime: info.ModTime(),+ size: info.Size(),+ exists: true,+ values: parsed,+ }}, nil})if err != nil {return envFileCacheEntry{}, err@@ -272,7 +275,7 @@ func validEnvFileCacheEntryInternal(entry envFileCacheEntry) bool {if info.IsDir() {return false}- if !entry.exists || !info.ModTime().Equal(entry.mtime) {+ if !entry.exists || !info.ModTime().Equal(entry.mtime) || info.Size() != entry.size {return false}// A chmod does not bump mtime, so confirm the file is still readable.@@ -846,11 +849,12 @@ func ParseComposeEnvOptions(workdir string, env EnvMap) (ComposeEnvOptions, errofunc ComposeFileEnvSelection(ctx context.Context, projectsDir, dir string) ([]string, error) {envMap := make(EnvMap)if strings.TrimSpace(projectsDir) != "" {- globalEnv, err := ParseProjectEnvFile(filepath.Join(projectsDir, GlobalEnvFileName), envMap)+ globalEnvPath := filepath.Join(projectsDir, GlobalEnvFileName)+ globalEnv, err := loadCachedEnvFileInternal(ctx, globalEnvFileCache, globalEnvPath+"\x00compose-file-selection", globalEnvPath, envMap)if err != nil {return nil, err}- maps.Copy(envMap, globalEnv)+ maps.Copy(envMap, globalEnv.values)}// COMPOSE_DISABLE_ENV_FILE skips the project .env; it is read only from thediff --git a/backend/pkg/projects/logs_demux.go b/backend/pkg/projects/logs_demux.goindex c5cd26f52..8c0a5d5fd 100644--- a/backend/pkg/projects/logs_demux.go+++ b/backend/pkg/projects/logs_demux.go@@ -5,6 +5,7 @@ import ("bytes""context""encoding/binary"+ "fmt""io""strings""sync"@@ -136,6 +137,10 @@ func (s *stderrMarkedDemuxedStreamInternal) ingestFrameInternal() {s.finishInternal(err)return}+ if header[0] == byte(stdcopy.Systemerr) {+ s.finishInternal(fmt.Errorf("error from daemon in stream: %s", payload))+ return+ }stream := kit.Ternary(header[0] == byte(stdcopy.Stderr), 1, 0)s.pending[stream] = append(s.pending[stream], payload...)diff --git a/backend/pkg/projects/pull_policy.go b/backend/pkg/projects/pull_policy.goindex 762198d7e..04aa40470 100644--- a/backend/pkg/projects/pull_policy.go+++ b/backend/pkg/projects/pull_policy.go@@ -178,16 +178,16 @@ func DecideDeployImageAction(svc composetypes.ServiceConfig, pullPolicyOverridecase policy == "never":return DeployImageDecision{RequireLocalOnly: true}case IsAlwaysPullPolicy(policy):- return DeployImageDecision{PullAlways: true}- case policy == "missing":- return DeployImageDecision{PullIfMissing: true}- case policy == "":- return DeployImageDecision{PullIfMissing: true, FallbackBuildOnPullFail: true}+ return DeployImageDecision{PullAlways: true, FallbackBuildOnPullFail: true}default:if window, ok := refreshWindowForPolicyInternal(svc, policy); ok {- return DeployImageDecision{PullIfStale: true, StaleAfter: window}+ return DeployImageDecision{+ PullIfStale: true,+ StaleAfter: window,+ FallbackBuildOnPullFail: true,+ }}- return DeployImageDecision{PullIfMissing: true}+ return DeployImageDecision{PullIfMissing: true, FallbackBuildOnPullFail: true}}}diff --git a/backend/pkg/projects/pull_policy_test.go b/backend/pkg/projects/pull_policy_test.goindex a655bb0cf..4baae9cc9 100644--- a/backend/pkg/projects/pull_policy_test.go+++ b/backend/pkg/projects/pull_policy_test.go@@ -240,8 +240,34 @@ func TestDecideDeployImageAction(t *testing.T) {decision := DecideDeployImageAction(svc, "")assert.True(t, decision.PullIfStale)assert.Equal(t, 6*time.Hour, decision.StaleAfter)+ assert.True(t, decision.FallbackBuildOnPullFail)assert.False(t, decision.Build)})++ t.Run("build service with deploy missing override falls back to build", func(t *testing.T) {+ svc := composetypes.ServiceConfig{Build: &composetypes.BuildConfig{Context: "."}}+ decision := DecideDeployImageAction(svc, "missing")+ assert.True(t, decision.PullIfMissing)+ assert.True(t, decision.FallbackBuildOnPullFail)+ assert.False(t, decision.Build)+ })++ t.Run("build service with always policy falls back to build", func(t *testing.T) {+ svc := composetypes.ServiceConfig{+ PullPolicy: "always",+ Build: &composetypes.BuildConfig{Context: "."},+ }+ decision := DecideDeployImageAction(svc, "")+ assert.True(t, decision.PullAlways)+ assert.True(t, decision.FallbackBuildOnPullFail)+ assert.False(t, decision.Build)+ })++ t.Run("non-build service never falls back to build", func(t *testing.T) {+ decision := DecideDeployImageAction(composetypes.ServiceConfig{}, "always")+ assert.True(t, decision.PullAlways)+ assert.False(t, decision.FallbackBuildOnPullFail)+ })}func TestShouldPullDeployImage(t *testing.T) {diff --git a/backend/pkg/scheduler/event_cleanup_job.go b/backend/pkg/scheduler/event_cleanup_job.goindex d95d2279c..c38988a8c 100644--- a/backend/pkg/scheduler/event_cleanup_job.go+++ b/backend/pkg/scheduler/event_cleanup_job.go@@ -11,6 +11,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/build""github.com/getarcaneapp/arcane/backend/v2/internal/event""github.com/getarcaneapp/arcane/backend/v2/internal/settings"+ "github.com/getarcaneapp/arcane/backend/v2/internal/updater""github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler/schedule")@@ -21,14 +22,22 @@ type EventCleanupJob struct {activityService *activity.ActivityServicebuildService *build.BuildServicesettingsService *settings.SettingsService+ updaterService *updater.UpdaterService}-func NewEventCleanupJob(eventService *event.EventService, activityService *activity.ActivityService, buildService *build.BuildService, settingsService *settings.SettingsService) *EventCleanupJob {+func NewEventCleanupJob(+ eventService *event.EventService,+ activityService *activity.ActivityService,+ buildService *build.BuildService,+ settingsService *settings.SettingsService,+ updaterService *updater.UpdaterService,+) *EventCleanupJob {return &EventCleanupJob{eventService: eventService,activityService: activityService,buildService: buildService,settingsService: settingsService,+ updaterService: updaterService,}}@@ -82,6 +91,18 @@ func (j *EventCleanupJob) Run(ctx context.Context) (schedulertypes.Outcome, erro"retentionDays", buildRetentionDays,"deleted", deletedBuilds)+ updateRetentionDays := j.settingsService.GetIntSetting(ctx, "activityHistoryRetentionDays", 30)+ deletedUpdates, err := j.updaterService.PruneHistory(ctx, updateRetentionDays)+ if err != nil {+ slog.ErrorContext(ctx, "Failed to prune auto-update history", "jobName", EventCleanupJobName, "error", err)+ return schedulertypes.Outcome{}, err+ }++ slog.InfoContext(ctx, "Auto-update history cleanup completed successfully",+ "jobName", EventCleanupJobName,+ "retentionDays", updateRetentionDays,+ "deleted", deletedUpdates)+return schedulertypes.Outcome{Status: schedulertypes.Succeeded}, nil}diff --git a/types/container/container.go b/types/container/container.goindex 57ff82654..75f8596ff 100644--- a/types/container/container.go+++ b/types/container/container.go@@ -1155,8 +1155,12 @@ func NewSummary(c container.Summary) Summary {ports := make([]Port, 0, len(c.Ports))for _, p := range c.Ports {+ hostIP := ""+ if p.IP.IsValid() {+ hostIP = p.IP.String()+ }ports = append(ports, Port{- IP: p.IP.String(),+ IP: hostIP,PrivatePort: int(p.PrivatePort),PublicPort: int(p.PublicPort),Type: p.Type,@@ -1249,8 +1253,12 @@ func mapInspectPorts(networkSettings *container.NetworkSettings) []Port {for _, b := range bindings {pub, _ := strconv.Atoi(b.HostPort)+ hostIP := ""+ if b.HostIP.IsValid() {+ hostIP = b.HostIP.String()+ }ports = append(ports, Port{- IP: b.HostIP.String(),+ IP: hostIP,PrivatePort: privatePort,PublicPort: pub,Type: typ,diff --git a/types/project/compose_cache.go b/types/project/compose_cache.goindex 43d492ccd..f3f2354a3 100644--- a/types/project/compose_cache.go+++ b/types/project/compose_cache.go@@ -1,10 +1,12 @@package project+import "time"+// ComposeCache stores values until their Compose configuration inputs change.// Fingerprints identify the settings and project identity used when loading.type ComposeCache[T any] interface {Get(projectID, fingerprint string) (T, bool)- Set(projectID, fingerprint, projectPath, projectsDirectory, composePath string, composeFiles, envFiles []string, value T) error+ Set(projectID, fingerprint, projectPath, projectsDirectory, composePath string, composeFiles, envFiles []string, loadedAt time.Time, value T) errorInvalidate(projectID string)}diff --git a/types/updater/durable.go b/types/updater/durable.goindex ce2fbca7b..7d95737a4 100644--- a/types/updater/durable.go+++ b/types/updater/durable.go@@ -10,6 +10,7 @@ type FrozenUpdateTarget struct {BaselineImageID stringBaselineStartedAt stringBaselineRestartCount int+ BaselineRunning boolDesiredImageRef stringDesiredDigest string}