package app import ( "context" "errors" "fmt" "time" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) func finalizeInvocation(runtime *invocationRuntime) (*RunSummary, error) { if containsStage(runtime.executed, "publish") { if err := runPostPublishCleanup( runtime.ctx, runtime.env, runtime.manifestPath, runtime.manifest, runtime.executed, runtime.dependencies.removeScopedDir, ); err != nil { return nil, persistPostPublishCleanupFailure( runtime.ctx, runtime.manifestStore, runtime.manifestPath, runtime.manifest, fmt.Errorf("post-publish cleanup incomplete: %w", err), runtime.dependencies.now(), ) } } return &RunSummary{ SessionID: runtime.identity.SessionID, RunID: runtime.identity.RunID, ManifestPath: runtime.manifestPath, StageNames: runtime.stageNames, Executed: runtime.executed, Skipped: runtime.skipped, }, nil } func persistPostPublishCleanupFailure( ctx context.Context, sessionStore manifest.Store, sessionManifestPath string, sessionManifest *manifest.Manifest, operationErr error, at time.Time, ) error { if operationErr == nil { return nil } if sessionManifest != nil { sessionManifest.RecordFailure(at, operationErr.Error()) } // Cleanup may have deliberately removed the selected run directory. The // committed remote snapshot remains authoritative, while the session // manifest records the outstanding local cleanup for the next invocation. sessionErr := sessionStore.Save(ctx, sessionManifestPath, sessionManifest) return errors.Join(operationErr, wrapTerminalPersistenceError("save terminal session manifest", sessionErr)) }