2026-05-02 10:37:38 -05:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-26 04:19:33 +00:00
|
|
|
"context"
|
2026-08-29 20:01:57 +00:00
|
|
|
"encoding/json"
|
2026-11-26 04:19:33 +00:00
|
|
|
"fmt"
|
2026-08-29 20:01:57 +00:00
|
|
|
"time"
|
2026-05-02 10:43:45 -05:00
|
|
|
|
2026-11-26 04:19:33 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
2026-05-02 10:43:45 -05:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
2026-08-29 20:01:57 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
2026-05-02 10:37:38 -05:00
|
|
|
)
|
|
|
|
|
|
2026-08-29 20:01:57 +00:00
|
|
|
func cloneManifestForPlan(source *manifest.Manifest, cfg *config.Config) (*manifest.Manifest, error) {
|
|
|
|
|
if source == nil {
|
|
|
|
|
created := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
|
|
|
|
created.Campaign = cfg.Session.Campaign
|
|
|
|
|
return created, nil
|
|
|
|
|
}
|
|
|
|
|
data, err := json.Marshal(source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var cloned manifest.Manifest
|
|
|
|
|
if err := json.Unmarshal(data, &cloned); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &cloned, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-30 12:44:26 +00:00
|
|
|
func modelPlannedStageRun(
|
|
|
|
|
model *manifest.Manifest,
|
|
|
|
|
selectedStage stage.Stage,
|
|
|
|
|
cfg *config.Config,
|
2026-09-04 01:01:46 +00:00
|
|
|
decision preExecutionDecision,
|
2026-08-30 12:44:26 +00:00
|
|
|
semanticConfig *manifest.SemanticConfigFingerprint,
|
|
|
|
|
) error {
|
2026-08-29 20:01:57 +00:00
|
|
|
prior := capturePriorStageOutcome(model, selectedStage.Name())
|
|
|
|
|
at := time.Now().UTC()
|
2026-09-04 01:01:46 +00:00
|
|
|
if _, err := beginStageAttempt(model, selectedStage.Name(), decision, at); err != nil {
|
|
|
|
|
return err
|
2026-08-29 20:01:57 +00:00
|
|
|
}
|
|
|
|
|
if reason := plannedSelfSkipReason(selectedStage.Name(), cfg); reason != "" {
|
2026-09-04 01:01:46 +00:00
|
|
|
_, err := applyCompletedStageOutcome(model, selectedStage.Name(), &stage.StageResult{
|
|
|
|
|
Disposition: stage.StageDispositionSkipped,
|
|
|
|
|
SkipReason: reason,
|
|
|
|
|
}, nil, semanticConfig, prior, at)
|
|
|
|
|
return err
|
2026-08-29 20:01:57 +00:00
|
|
|
}
|
2026-09-04 01:01:46 +00:00
|
|
|
_, err := applyCompletedStageOutcome(model, selectedStage.Name(), &stage.StageResult{}, nil, semanticConfig, prior, at)
|
|
|
|
|
return err
|
2026-08-29 20:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func plannedSelfSkipReason(stageName string, cfg *config.Config) string {
|
|
|
|
|
if stageName == "extract" && cfg != nil && cfg.Pipeline != nil &&
|
|
|
|
|
(cfg.Pipeline.Notarius == nil || !cfg.Pipeline.Notarius.Enabled) {
|
|
|
|
|
return "notarius_disabled"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-11-26 04:19:33 +00:00
|
|
|
|
|
|
|
|
type workProjection struct {
|
|
|
|
|
Stages []projectedStage
|
|
|
|
|
Model *manifest.Manifest
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type projectedStage struct {
|
|
|
|
|
Name string
|
|
|
|
|
Decision preExecutionDecision
|
|
|
|
|
Reason string
|
|
|
|
|
Analysis *stage.AnalyzeResumeSummary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func projectSelectedWork(ctx context.Context, cfg *config.Config, plan BoundedPlan, force bool, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet, store artifacts.Store, snapshot *manifest.Manifest) (*workProjection, error) {
|
|
|
|
|
model, err := cloneManifestForPlan(snapshot, cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: clone session state: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stages := plan.Stages()
|
|
|
|
|
stageEnv := &stage.Env{
|
|
|
|
|
Config: cfg, SelectedArtifactKeys: append([]string(nil), selectedArtifacts...),
|
|
|
|
|
EffectiveArtifacts: effective, ArtifactStore: store, Force: force,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
projection := &workProjection{Model: model}
|
|
|
|
|
for _, selectedStage := range stages {
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
return projection, err
|
|
|
|
|
}
|
|
|
|
|
semanticConfig, err := currentStageSemanticConfig(selectedStage, stageEnv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: fingerprint semantic configuration for stage %q: %w", selectedStage.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
decision := decideStageExecution(recordedStageStatus(model, selectedStage.Name()), force, nil)
|
|
|
|
|
var validation *stage.ResumeValidation
|
|
|
|
|
if decision.action == stageActionSkip {
|
|
|
|
|
checked, validationErr := evaluateStageResume(ctx, selectedStage, stageEnv, model, semanticConfig)
|
|
|
|
|
if validationErr != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
|
|
|
|
}
|
|
|
|
|
validation = checked
|
|
|
|
|
decision = decideStageExecution(recordedStageStatus(model, selectedStage.Name()), force, checked)
|
|
|
|
|
}
|
|
|
|
|
if _, err := applyPreExecutionDecision(model, selectedStage.Name(), decision, time.Now().UTC()); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: model resume invalidation for stage %q: %w", selectedStage.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
if decision.action == stageActionRun && selectedStage.Name() == "analyze" {
|
|
|
|
|
if validator, ok := selectedStage.(stage.ResumeValidator); ok {
|
|
|
|
|
checked, validationErr := validator.ValidateResume(ctx, stageEnv, model)
|
|
|
|
|
if validationErr != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
|
|
|
|
}
|
|
|
|
|
checked = checked.Normalized()
|
|
|
|
|
validation = &checked
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reason := decision.selectedStaleReason
|
|
|
|
|
if reason == "" {
|
|
|
|
|
switch {
|
|
|
|
|
case force:
|
|
|
|
|
reason = "forced"
|
|
|
|
|
case decision.action == stageActionSkip:
|
|
|
|
|
reason = "recorded result is reusable"
|
|
|
|
|
default:
|
|
|
|
|
reason = "no reusable successful result"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
entry := projectedStage{Name: selectedStage.Name(), Decision: decision, Reason: reason}
|
|
|
|
|
if validation != nil {
|
|
|
|
|
entry.Analysis = validation.Analyze
|
|
|
|
|
}
|
|
|
|
|
projection.Stages = append(projection.Stages, entry)
|
|
|
|
|
if decision.action == stageActionRun {
|
|
|
|
|
if err := modelPlannedStageRun(model, selectedStage, cfg, decision, semanticConfig); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("plan: model stage %q: %w", selectedStage.Name(), err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return projection, ctx.Err()
|
|
|
|
|
}
|