Share read-only planning and analysis closure
This commit is contained in:
@@ -254,6 +254,9 @@ Check: `go test ./internal/stage ./internal/config`.
|
||||
|
||||
## Stage 6: Extract reusable read-only planning and closure
|
||||
|
||||
**Status: Complete.** Focused planning and stage checks pass. The full
|
||||
application check still has the offline fixture failures recorded under Stage 4.
|
||||
|
||||
**Purpose:** Reuse the existing projected plan for early inspection while keeping
|
||||
actual execution authoritative.
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"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"
|
||||
@@ -56,3 +59,84 @@ func plannedSelfSkipReason(stageName string, cfg *config.Config) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
@@ -103,71 +102,22 @@ func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionI
|
||||
return nil, fmt.Errorf("plan: %w", err)
|
||||
}
|
||||
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
||||
model, err := cloneManifestForPlan(m, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plan: clone session state: %w", err)
|
||||
projection, err := projectSelectedWork(ctx, cfg, input.plan, input.Force.Value, selectedArtifacts, effective, store, m)
|
||||
if projection == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stages := input.plan.Stages()
|
||||
stageEnv := &stage.Env{
|
||||
Config: cfg, SelectedArtifactKeys: append([]string(nil), selectedArtifacts...),
|
||||
EffectiveArtifacts: effective, ArtifactStore: store, Force: input.Force.Value,
|
||||
}
|
||||
|
||||
report := &sessionPlanReport{Campaign: config.CampaignID(cfg.Campaign), SessionID: cfg.Session.SessionID, Stages: []plannedStage{}, Publication: []plannedPublication{}}
|
||||
for _, selectedStage := range stages {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return report, 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()), input.Force.Value, 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()), input.Force.Value, 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 input.Force.Value:
|
||||
reason = "forced"
|
||||
case decision.action == stageActionSkip:
|
||||
reason = "recorded result is reusable"
|
||||
default:
|
||||
reason = "no reusable successful result"
|
||||
}
|
||||
}
|
||||
entry := plannedStage{Name: selectedStage.Name(), Action: string(decision.action), Reason: reason}
|
||||
if validation != nil && validation.Analyze != nil {
|
||||
entry.Analysis = analysisPlanReport(validation.Analyze)
|
||||
for _, item := range projection.Stages {
|
||||
entry := plannedStage{Name: item.Name, Action: string(item.Decision.action), Reason: item.Reason}
|
||||
if item.Analysis != nil {
|
||||
entry.Analysis = analysisPlanReport(item.Analysis)
|
||||
}
|
||||
report.Stages = append(report.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
model := projection.Model
|
||||
if input.plan.Contains("publish") && cfg.Pipeline.Publish != nil && (cfg.Pipeline.Publish.Enabled == nil || *cfg.Pipeline.Publish.Enabled) {
|
||||
catalog, err := buildHelperArtifactCatalog(cfg, model)
|
||||
if err != nil {
|
||||
|
||||
@@ -50,6 +50,64 @@ func TestPlanDoesNotCreateWorkdir(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
type observingArtifactStore struct {
|
||||
artifacts.Store
|
||||
pathLookups int
|
||||
}
|
||||
|
||||
func (s *observingArtifactStore) SessionPathsFor(campaign, sessionID string) artifacts.SessionPaths {
|
||||
s.pathLookups++
|
||||
return s.Store.SessionPathsFor(campaign, sessionID)
|
||||
}
|
||||
|
||||
func TestProjectionUsesInjectedSnapshotAndSchedulesPrerequisites(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
||||
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{
|
||||
"prerequisite": {Enabled: false, PromptID: "parent", OutputPath: "artifacts/parent.md"},
|
||||
"target": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"prerequisite"}},
|
||||
}
|
||||
selection := []string{"target"}
|
||||
effective, err := resolveEffectiveArtifacts(cfg, selection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, err := BuildBoundedPlan("analyze", "analyze")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
||||
snapshot.Campaign = cfg.Session.Campaign
|
||||
snapshot.MarkStageSucceeded("analyze", time.Now().UTC(), nil)
|
||||
snapshot.Stages["analyze"].AnalyzeStateVersion = manifest.AnalyzeStateContractVersion
|
||||
snapshot.Stages["analyze"].AnalyzeArtifacts = map[string]manifest.AnalyzeArtifactRecord{"target": {Key: "target", Status: manifest.AnalyzeArtifactMissing, ProducerRunID: "prior", UpdatedAt: time.Now().UTC()}}
|
||||
before, err := cloneManifestForPlan(snapshot, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := &observingArtifactStore{Store: artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)}
|
||||
projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, store, snapshot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.pathLookups == 0 {
|
||||
t.Fatal("projection did not use injected store")
|
||||
}
|
||||
if !reflect.DeepEqual(snapshot, before) {
|
||||
t.Fatal("projection mutated authoritative manifest")
|
||||
}
|
||||
if len(projection.Stages) != 1 || projection.Stages[0].Analysis == nil {
|
||||
t.Fatalf("projection = %+v", projection)
|
||||
}
|
||||
work := projection.Stages[0].Analysis.ExecutionOrder
|
||||
if len(work) != 2 || work[0].Key != "prerequisite" || work[1].Key != "target" {
|
||||
t.Fatalf("scheduled work = %+v", work)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanShowsRunAndSkipFromManifest(t *testing.T) {
|
||||
workspaceRoot := t.TempDir()
|
||||
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
|
||||
|
||||
@@ -63,11 +63,7 @@ func planAnalyzeWork(
|
||||
}
|
||||
targets := effective.Keys()
|
||||
targetSet := stringSet(targets)
|
||||
closure, err := analyzeTargetDependencyClosure(configured, targets)
|
||||
if err != nil {
|
||||
return analyzeWorkPlan{}, err
|
||||
}
|
||||
order, err := orderConfiguredAnalyzeArtifacts(configured)
|
||||
order, err := AnalyzeTargetClosure(configured, targets)
|
||||
if err != nil {
|
||||
return analyzeWorkPlan{}, err
|
||||
}
|
||||
@@ -100,11 +96,8 @@ func planAnalyzeWork(
|
||||
invalidated[state.Key] = struct{}{}
|
||||
}
|
||||
}
|
||||
scheduled := make(map[string]bool, len(closure))
|
||||
scheduled := make(map[string]bool, len(order))
|
||||
for _, key := range order {
|
||||
if _, included := closure[key]; !included {
|
||||
continue
|
||||
}
|
||||
state, ok := reconciled[key]
|
||||
if !ok {
|
||||
return analyzeWorkPlan{}, fmt.Errorf("analysis reconciliation is missing configured artifact %q", key)
|
||||
@@ -150,10 +143,11 @@ func planAnalyzeWork(
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
func analyzeTargetDependencyClosure(
|
||||
// AnalyzeTargetClosure returns targets and their prerequisites in work order.
|
||||
func AnalyzeTargetClosure(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
targets []string,
|
||||
) (map[string]struct{}, error) {
|
||||
) ([]string, error) {
|
||||
closure := make(map[string]struct{}, len(targets))
|
||||
var visit func(string) error
|
||||
visit = func(key string) error {
|
||||
@@ -177,7 +171,17 @@ func analyzeTargetDependencyClosure(
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return closure, nil
|
||||
order, err := orderConfiguredAnalyzeArtifacts(configured)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]string, 0, len(closure))
|
||||
for _, key := range order {
|
||||
if _, included := closure[key]; included {
|
||||
result = append(result, key)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func reconciledAnalyzeRecords(
|
||||
|
||||
Reference in New Issue
Block a user