package app import ( "context" "fmt" "strings" "time" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" "gitea.maximumdirect.net/eric/narratio/internal/stage" ) type stageAction string const ( stageActionRun stageAction = "run" stageActionSkip stageAction = "skip" ) type stageDecision struct { Stage stage.Stage Action stageAction } const ( staleReasonForcedReplacement = "upstream stage was force-run" staleReasonChangedResult = "upstream stage result changed" staleReasonFailure = "upstream stage failed" staleReasonSelfSkip = "upstream stage self-skipped" staleReasonNotResumable = "upstream stage result was not resumable" ) type priorStageOutcome struct { exists bool status manifest.StageStatus skipReason string outputs int } func decideStageActions(stages []stage.Stage, m *manifest.Manifest, force bool) []stageDecision { out := make([]stageDecision, 0, len(stages)) for _, s := range stages { out = append(out, stageDecision{ Stage: s, Action: decideStageAction(s, m, force), }) } return out } func decideStageAction(s stage.Stage, m *manifest.Manifest, force bool) stageAction { // TODO: incorporate stale detection once checksum/input change tracking is implemented. if !force && stageSucceeded(m, s.Name()) { return stageActionSkip } return stageActionRun } func stageSucceeded(m *manifest.Manifest, name string) bool { if m == nil || m.Stages == nil { return false } sr := m.Stages[name] return sr != nil && sr.Status == manifest.StatusSucceeded } func capturePriorStageOutcome(m *manifest.Manifest, name string) priorStageOutcome { if m == nil || m.Stages == nil || m.Stages[name] == nil { return priorStageOutcome{} } record := m.Stages[name] outcome := priorStageOutcome{ exists: true, status: record.Status, outputs: len(record.Outputs), } if record.Error != nil && record.Error.Code == "skipped" { outcome.skipReason = record.Error.Message } return outcome } func (o priorStageOutcome) isSameSelfSkip(reason string) bool { return o.exists && o.status == manifest.StatusSkipped && o.outputs == 0 && o.skipReason == strings.TrimSpace(reason) } func loadManifestIfPresent(ctx context.Context, cfg *config.Config) (*manifest.Manifest, error) { path := artifacts.SessionManifestPathForCampaign( cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID, ) exists, err := fileExists(path) if err != nil { return nil, fmt.Errorf("check manifest %q: %w", path, err) } if !exists { return nil, nil } store := &manifest.LocalStore{} m, err := store.Load(ctx, path) if err != nil { return nil, fmt.Errorf("load manifest %q: %w", path, err) } return m, nil } func canonicalStageNames() []string { all := stage.All() out := make([]string, 0, len(all)) for _, s := range all { if s == nil { continue } out = append(out, s.Name()) } return out } func downstreamStageNames(stageName string) []string { names := canonicalStageNames() for i, name := range names { if name != stageName { continue } return append([]string(nil), names[i+1:]...) } return nil } func invalidateDownstreamSucceededStagesWithReason(m *manifest.Manifest, upstreamStage string, at time.Time, reason string) []string { if m == nil || m.Stages == nil { return nil } invalidated := make([]string, 0) for _, downstream := range downstreamStageNames(upstreamStage) { sr := m.Stages[downstream] if sr == nil || sr.Status != manifest.StatusSucceeded { continue } m.MarkStageStale(downstream, at, reason) invalidated = append(invalidated, downstream) } return invalidated }