Share artifact dependency queries across consumers
This commit is contained in:
@@ -70,6 +70,7 @@ func TestProjectionUsesInjectedSnapshotAndSchedulesPrerequisites(t *testing.T) {
|
||||
"prerequisite": {Enabled: false, PromptID: "parent", OutputPath: "artifacts/parent.md"},
|
||||
"target": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"prerequisite"}},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
selection := []string{"target"}
|
||||
effective, err := resolveEffectiveArtifacts(cfg, selection)
|
||||
if err != nil {
|
||||
@@ -310,3 +311,12 @@ func planFixtureFiles(t *testing.T, root string) []string {
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
func rebuildTestArtifactGraph(t *testing.T, cfg *config.Config) {
|
||||
t.Helper()
|
||||
graph, err := config.BuildArtifactGraph(cfg.ArtifactCatalog.Artifacts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ArtifactCatalog.Graph = graph
|
||||
}
|
||||
|
||||
@@ -40,7 +40,11 @@ func enabledInspectionTargets(cfg *config.Config, sessionID string) ([]preflight
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys, err = artifacts.AnalyzeTargetClosure(cfg.ConcreteArtifacts(), selected.Keys())
|
||||
graph, err := cfg.ArtifactGraph()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys, err = graph.Prerequisites(selected.Keys(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ func TestInspectionTargetsUseProjectedWorkAndDefinitionClosure(t *testing.T) {
|
||||
"child": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"parent"}, Inputs: map[string]config.ScriptoriumInputConfig{"transcript": {Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}, "notes": {Source: "narratio.artifact.parent", Required: false}}},
|
||||
"unrelated": {Enabled: false, PromptID: "other", OutputPath: "artifacts/other.md"},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
selection := []string{"child"}
|
||||
effective, err := resolveEffectiveArtifacts(cfg, selection)
|
||||
if err != nil {
|
||||
|
||||
@@ -106,7 +106,7 @@ func referenceHistoryRequirements(cfg *config.Config, effective artifacts.Effect
|
||||
continue
|
||||
}
|
||||
if item.Analysis == nil {
|
||||
return artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective)
|
||||
return artifacts.CollectPreviousArtifactRequirementsForConfig(cfg, effective)
|
||||
}
|
||||
for _, work := range item.Analysis.ExecutionOrder {
|
||||
keys = append(keys, work.Key)
|
||||
|
||||
@@ -31,6 +31,7 @@ func TestBoundedAnalysisPreparesReferencesForSuppliedTranscript(t *testing.T) {
|
||||
cfg.Session.Inputs.AudioFiles = nil
|
||||
cfg.Session.Inputs.AudioS3 = nil
|
||||
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{"recap": {Enabled: true, PromptID: "dnd.recap", OutputPath: "artifacts/recap.md", Inputs: map[string]config.ScriptoriumInputConfig{"party": {Source: artifactpolicy.SourceInputParty, Required: true}, "transcript": {Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}}}}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
paths := artifacts.NewLocalStore(root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
transcript := filepath.Join(paths.Root, filepath.FromSlash(artifacts.TranscriptPathFinalTrimmed))
|
||||
if err := os.MkdirAll(filepath.Dir(transcript), 0755); err != nil {
|
||||
@@ -105,6 +106,7 @@ func TestBoundedAnalysisConsumesSuppliedProductsWithoutTranscripts(t *testing.T)
|
||||
"old_history": {Source: artifactpolicy.PreviousSessionSourceID("parent"), Required: true},
|
||||
}},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
if extraction {
|
||||
source, relative, body = artifacts.ExtractionArtifactSourceID("entities"), "artifacts/entities.json", `{"entities":[]}`
|
||||
cfg.Pipeline.Notarius = &config.NotariusConfig{Enabled: true, Outputs: map[string]config.NotariusOutputConfig{"entities": {LaneID: "entities"}}}
|
||||
@@ -116,10 +118,12 @@ func TestBoundedAnalysisConsumesSuppliedProductsWithoutTranscripts(t *testing.T)
|
||||
"party": {Source: artifactpolicy.SourceInputParty, Required: true},
|
||||
},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
if !extraction {
|
||||
child := cfg.ArtifactCatalog.Artifacts["child"]
|
||||
child.DependsOn = []string{"parent"}
|
||||
cfg.ArtifactCatalog.Artifacts["child"] = child
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
}
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
path := filepath.Join(paths.Root, relative)
|
||||
@@ -174,6 +178,7 @@ func TestBoundedAnalysisConsumesSuppliedProductsWithoutTranscripts(t *testing.T)
|
||||
child := cfg.ArtifactCatalog.Artifacts["child"]
|
||||
child.Inputs["missing"] = config.ScriptoriumInputConfig{Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}
|
||||
cfg.ArtifactCatalog.Artifacts["child"] = child
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
input := &executionInput{plan: plan}
|
||||
input.Force.Value = true
|
||||
preview, err = buildSessionPlan(ctx, cfg, input, nil, effective, commandDependencies{})
|
||||
@@ -203,6 +208,7 @@ func TestBoundedAnalysisContinuesAfterLastSuppliedProductIsReplaced(t *testing.T
|
||||
"party": {Source: artifactpolicy.SourceInputParty, Required: true},
|
||||
}},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
remote := &storage.FakeBackend{}
|
||||
seedCommittedRestoreSnapshot(t, cfg, remote, "20260519T010203Z-a1b2c3d4", map[string][]byte{"artifacts/session_recap.md": []byte("supplied recap\n")})
|
||||
if _, err := restoreSessionState(ctx, cfg, RestorePlanOptions{}, normalizeCommandDependencies(restoreCommandDependencies(remote))); err != nil {
|
||||
@@ -253,6 +259,7 @@ func TestBoundedReferencePreparationRetainsHistoryForCurrentLocalPrerequisites(t
|
||||
"parent": {Source: artifacts.ConfiguredArtifactSourceID("session_recap"), Required: true},
|
||||
}},
|
||||
}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
remote := &storage.FakeBackend{}
|
||||
currentID := cfg.Session.SessionID
|
||||
cfg.Session.SessionID = "2026-05-02"
|
||||
@@ -299,6 +306,7 @@ func TestFullRunWithSuppliedTranscriptPreparesAndRendersSelectedVariant(t *testi
|
||||
cfg.Session.Inputs.AudioS3 = nil
|
||||
cfg.Pipeline.Render.Variants = []string{"trimmed"}
|
||||
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{"recap": {Enabled: true, PromptID: "dnd.recap", OutputPath: "artifacts/recap.md", Inputs: map[string]config.ScriptoriumInputConfig{"party": {Source: artifactpolicy.SourceInputParty, Required: true}, "transcript": {Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}}}}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
fake := &storage.FakeBackend{}
|
||||
seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/final.trimmed.json": []byte(`{"segments":[]}`)})
|
||||
if _, err := restoreSessionState(context.Background(), cfg, RestorePlanOptions{}, normalizeCommandDependencies(restoreCommandDependencies(fake))); err != nil {
|
||||
@@ -380,6 +388,7 @@ func TestBoundedPlanReportsReferencePreparationAndMissingInput(t *testing.T) {
|
||||
cfg.Session.Inputs.AudioDir = ""
|
||||
cfg.Session.Inputs.AudioFiles = nil
|
||||
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{"recap": {Enabled: true, PromptID: "dnd.recap", OutputPath: "artifacts/recap.md", Inputs: map[string]config.ScriptoriumInputConfig{"transcript": {Source: artifacts.ArtifactTranscriptFinal, Required: true}}}}
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
paths := artifacts.NewLocalStore(root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
transcript := filepath.Join(paths.Root, filepath.FromSlash(artifacts.TranscriptPathFinalTrimmed))
|
||||
if err := os.MkdirAll(filepath.Dir(transcript), 0755); err != nil {
|
||||
@@ -426,6 +435,7 @@ func TestBoundedPlanReportsReferencePreparationAndMissingInput(t *testing.T) {
|
||||
recap := cfg.ArtifactCatalog.Artifacts["recap"]
|
||||
recap.Inputs["history"] = config.ScriptoriumInputConfig{Source: artifactpolicy.PreviousSessionSourceID("recap"), Required: true}
|
||||
cfg.ArtifactCatalog.Artifacts["recap"] = recap
|
||||
rebuildTestArtifactGraph(t, cfg)
|
||||
report, err = buildSessionPlan(context.Background(), cfg, input, nil, effective, commandDependencies{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -469,7 +469,7 @@ func buildPreviousCacheRestoreActions(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirements(cfg.ConcreteArtifacts(), effective)
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirementsForConfig(cfg, effective)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -544,7 +544,7 @@ func needsObjectStoreForRun(cfg *config.Config, stages []stage.Stage, effectiveS
|
||||
return true, nil
|
||||
}
|
||||
if stageRequested("prepare") {
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective)
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirementsForConfig(cfg, effective)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func composeInvocation(runtime *invocationRuntime, stages []stage.Stage) error {
|
||||
return err
|
||||
}
|
||||
if stagesContainAny(stages, "prepare") && !runtime.opts.Force && suppliedCompletion(runtime.cfg, runtime.manifest)["transcribe"] {
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(runtime.cfg), runtime.effectiveArtifacts)
|
||||
requirements, err := artifacts.CollectPreviousArtifactRequirementsForConfig(runtime.cfg, runtime.effectiveArtifacts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ func inspectSessionState(ctx context.Context, cfg *config.Config, status, remote
|
||||
effective, err := resolveEffectiveArtifacts(cfg, nil)
|
||||
fail("artifacts", err)
|
||||
if err == nil {
|
||||
requirements, collectErr := artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective)
|
||||
requirements, collectErr := artifacts.CollectPreviousArtifactRequirementsForConfig(cfg, effective)
|
||||
fail("artifacts", collectErr)
|
||||
if collectErr != nil {
|
||||
return report, errors.Join(failures...)
|
||||
|
||||
@@ -57,16 +57,7 @@ func New(edges map[string][]string) (*Graph, error) {
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
g.dependencies[key] = nil
|
||||
seen := make(map[string]bool)
|
||||
for _, value := range edges[key] {
|
||||
dep := strings.TrimSpace(value)
|
||||
if dep == "" || seen[dep] {
|
||||
continue
|
||||
}
|
||||
seen[dep] = true
|
||||
g.dependencies[key] = append(g.dependencies[key], dep)
|
||||
}
|
||||
sort.Strings(g.dependencies[key])
|
||||
g.dependencies[key] = Normalize(edges[key])
|
||||
for _, dep := range g.dependencies[key] {
|
||||
if _, ok := edges[dep]; !ok {
|
||||
return nil, &Error{Kind: UnknownDependency, Node: key, Dependency: dep}
|
||||
@@ -130,13 +121,30 @@ func (h keyHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
||||
func (h *keyHeap) Push(v any) { *h = append(*h, v.(string)) }
|
||||
func (h *keyHeap) Pop() any { old := *h; n := len(old); v := old[n-1]; *h = old[:n-1]; return v }
|
||||
|
||||
// Normalize returns sorted, unique non-empty dependency keys.
|
||||
func Normalize(values []string) []string {
|
||||
seen := make(map[string]bool, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value != "" {
|
||||
seen[value] = true
|
||||
}
|
||||
}
|
||||
result := make([]string, 0, len(seen))
|
||||
for key := range seen {
|
||||
result = append(result, key)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
// Dependencies returns a copy of the canonical direct prerequisites.
|
||||
func (g *Graph) Dependencies(key string) []string {
|
||||
return append([]string(nil), g.dependencies[key]...)
|
||||
return append([]string{}, g.dependencies[key]...)
|
||||
}
|
||||
|
||||
// Order returns all keys in deterministic prerequisite-first order.
|
||||
func (g *Graph) Order() []string { return append([]string(nil), g.order...) }
|
||||
func (g *Graph) Order() []string { return append([]string{}, g.order...) }
|
||||
|
||||
// Prerequisites returns the target closure in work order, stopping at stop keys.
|
||||
func (g *Graph) Prerequisites(targets []string, stop map[string]bool) ([]string, error) {
|
||||
@@ -164,7 +172,7 @@ func (g *Graph) Prerequisites(targets []string, stop map[string]bool) ([]string,
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var result []string
|
||||
result := make([]string, 0, len(included))
|
||||
for _, key := range g.order {
|
||||
if included[key] {
|
||||
result = append(result, key)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package artifactdeps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -18,6 +19,10 @@ func TestGraphQueries(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
serialized, err := json.Marshal(g.Dependencies("root"))
|
||||
if err != nil || string(serialized) != "[]" {
|
||||
t.Fatalf("empty dependency JSON = %s, %v", serialized, err)
|
||||
}
|
||||
wantOrder := []string{"other", "root", "left", "right", "end"}
|
||||
if got := g.Order(); !reflect.DeepEqual(got, wantOrder) {
|
||||
t.Fatalf("order = %v", got)
|
||||
|
||||
@@ -1,124 +1,27 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
)
|
||||
|
||||
// AnalyzeTargetClosure returns targets and their prerequisites in work order.
|
||||
func AnalyzeTargetClosure(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
targets []string,
|
||||
) ([]string, error) {
|
||||
closure := make(map[string]struct{}, len(targets))
|
||||
var visit func(string) error
|
||||
visit = func(key string) error {
|
||||
if _, seen := closure[key]; seen {
|
||||
return nil
|
||||
}
|
||||
artifactCfg, ok := configured[key]
|
||||
if !ok {
|
||||
return fmt.Errorf("selected artifact %q is not configured", key)
|
||||
}
|
||||
closure[key] = struct{}{}
|
||||
for _, dependency := range NormalizedDependencyKeys(artifactCfg.DependsOn) {
|
||||
if err := visit(dependency); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, target := range targets {
|
||||
if err := visit(target); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
order, err := OrderConfiguredArtifacts(configured)
|
||||
func AnalyzeTargetClosure(configured map[string]config.ScriptoriumArtifactConfig, targets []string) ([]string, error) {
|
||||
graph, err := config.BuildArtifactGraph(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
|
||||
}
|
||||
|
||||
type artifactReadyQueue []string
|
||||
|
||||
func (q artifactReadyQueue) Len() int { return len(q) }
|
||||
func (q artifactReadyQueue) Less(i, j int) bool { return q[i] < q[j] }
|
||||
func (q artifactReadyQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
|
||||
func (q *artifactReadyQueue) Push(value any) { *q = append(*q, value.(string)) }
|
||||
func (q *artifactReadyQueue) Pop() any {
|
||||
last := len(*q) - 1
|
||||
value := (*q)[last]
|
||||
(*q)[last] = ""
|
||||
*q = (*q)[:last]
|
||||
return value
|
||||
return graph.Prerequisites(targets, nil)
|
||||
}
|
||||
|
||||
// OrderConfiguredArtifacts returns configured keys in prerequisite-first order.
|
||||
func OrderConfiguredArtifacts(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
) ([]string, error) {
|
||||
indegree := make(map[string]int, len(configured))
|
||||
edges := make(map[string][]string, len(configured))
|
||||
for key := range configured {
|
||||
indegree[key] = 0
|
||||
func OrderConfiguredArtifacts(configured map[string]config.ScriptoriumArtifactConfig) ([]string, error) {
|
||||
graph, err := config.BuildArtifactGraph(configured)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, artifactCfg := range configured {
|
||||
for _, dependency := range NormalizedDependencyKeys(artifactCfg.DependsOn) {
|
||||
if _, ok := configured[dependency]; !ok {
|
||||
return nil, fmt.Errorf("configured artifact %q depends on unknown artifact %q", key, dependency)
|
||||
}
|
||||
edges[dependency] = append(edges[dependency], key)
|
||||
indegree[key]++
|
||||
}
|
||||
}
|
||||
ready := make(artifactReadyQueue, 0, len(indegree))
|
||||
for key, degree := range indegree {
|
||||
if degree == 0 {
|
||||
ready = append(ready, key)
|
||||
}
|
||||
}
|
||||
heap.Init(&ready)
|
||||
order := make([]string, 0, len(indegree))
|
||||
for len(ready) > 0 {
|
||||
key := heap.Pop(&ready).(string)
|
||||
order = append(order, key)
|
||||
for _, dependent := range edges[key] {
|
||||
indegree[dependent]--
|
||||
if indegree[dependent] == 0 {
|
||||
heap.Push(&ready, dependent)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(order) != len(configured) {
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifacts dependencies must not contain cycles")
|
||||
}
|
||||
return order, nil
|
||||
return graph.Order(), nil
|
||||
}
|
||||
|
||||
// NormalizedDependencyKeys deduplicates and sorts dependency keys.
|
||||
func NormalizedDependencyKeys(values []string) []string {
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed != "" {
|
||||
seen[trimmed] = struct{}{}
|
||||
}
|
||||
}
|
||||
result := make([]string, 0, len(seen))
|
||||
for value := range seen {
|
||||
result = append(result, value)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
func NormalizedDependencyKeys(values []string) []string { return artifactdeps.Normalize(values) }
|
||||
|
||||
@@ -33,6 +33,22 @@ func CollectPreviousArtifactRequirements(
|
||||
return PreviousArtifactRequirementsForKeys(artifactsCfg, artifactNames)
|
||||
}
|
||||
|
||||
// CollectPreviousArtifactRequirementsForConfig uses the invocation's derived graph.
|
||||
func CollectPreviousArtifactRequirementsForConfig(cfg *config.Config, effective EffectiveArtifactSet) ([]PreviousArtifactRequirement, error) {
|
||||
if len(effective.Keys()) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
graph, err := cfg.ArtifactGraph()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys, err := graph.Prerequisites(effective.Keys(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return PreviousArtifactRequirementsForKeys(cfg.ConcreteArtifacts(), keys)
|
||||
}
|
||||
|
||||
// PreviousArtifactRequirementsForKeys collects history for already planned
|
||||
// artifact keys, including local checkpoints whose input evidence remains needed.
|
||||
// Structural prerequisite collection uses the wrapper above.
|
||||
|
||||
@@ -143,6 +143,11 @@ func finishArtifactCatalog(cfg *PipelineConfig, catalog *ArtifactCatalog, party
|
||||
if err := validateConcreteArtifacts(catalog.Artifacts, cfg.Notarius); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
graph, err := BuildArtifactGraph(catalog.Artifacts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
catalog.Graph = graph
|
||||
if err := deriveFamilyPublishRules(cfg, catalog); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -61,6 +61,9 @@ characters:
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected := selectedContext.ArtifactCatalog
|
||||
if selected.Graph == nil || !reflect.DeepEqual(selected.Graph.Order(), []string{"character_notes_other"}) {
|
||||
t.Fatalf("derived dependency graph = %v", selected.Graph)
|
||||
}
|
||||
if len(selected.Artifacts) != 1 || selected.Artifacts["character_notes_other"].Vars["player"] != "Selected Player" {
|
||||
t.Fatalf("selected catalog = %+v", selected)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package config
|
||||
|
||||
import configkit "go.maximumdirect.net/configkit"
|
||||
import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// Config is the resolved combined configuration from pipeline.yml,
|
||||
// campaign.yml, and session.yml.
|
||||
@@ -29,6 +32,14 @@ func (cfg *Config) ConcreteArtifacts() map[string]ScriptoriumArtifactConfig {
|
||||
return cfg.ArtifactCatalog.Artifacts
|
||||
}
|
||||
|
||||
// ArtifactGraph returns the derived dependency graph, or builds one for programmatic catalogs.
|
||||
func (cfg *Config) ArtifactGraph() (*artifactdeps.Graph, error) {
|
||||
if cfg != nil && cfg.ArtifactCatalog != nil && cfg.ArtifactCatalog.Graph != nil {
|
||||
return cfg.ArtifactCatalog.Graph, nil
|
||||
}
|
||||
return BuildArtifactGraph(cfg.ConcreteArtifacts())
|
||||
}
|
||||
|
||||
// PublicationOutputs returns publication actions derived for the selected roster.
|
||||
func (cfg *Config) PublicationOutputs() []PublishOutputRule {
|
||||
if cfg == nil || cfg.ArtifactCatalog == nil {
|
||||
@@ -66,6 +77,7 @@ type PipelineConfig struct {
|
||||
// ArtifactCatalog contains domain products derived from completed pipeline
|
||||
// configuration and one selected canonical roster.
|
||||
type ArtifactCatalog struct {
|
||||
Graph *artifactdeps.Graph `yaml:"-" json:"-"`
|
||||
Artifacts map[string]ScriptoriumArtifactConfig
|
||||
Families ArtifactFamilyCatalog
|
||||
PublishOutputs []PublishOutputRule
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
)
|
||||
|
||||
@@ -263,76 +264,51 @@ func validateScriptoriumInputSource(
|
||||
// graph independently of execution selection. Disabled artifacts remain valid
|
||||
// prerequisites for explicit selections and therefore participate in cycles.
|
||||
func ValidateScriptoriumArtifactDependencies(artifacts map[string]ScriptoriumArtifactConfig) error {
|
||||
if len(artifacts) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := BuildArtifactGraph(artifacts)
|
||||
return err
|
||||
}
|
||||
|
||||
// BuildArtifactGraph validates definitions and builds their immutable dependency view.
|
||||
func BuildArtifactGraph(artifacts map[string]ScriptoriumArtifactConfig) (*artifactdeps.Graph, error) {
|
||||
nodes := make([]string, 0, len(artifacts))
|
||||
for name := range artifacts {
|
||||
nodes = append(nodes, name)
|
||||
}
|
||||
sort.Strings(nodes)
|
||||
|
||||
graph := make(map[string][]string, len(artifacts))
|
||||
edges := make(map[string][]string, len(artifacts))
|
||||
for _, name := range nodes {
|
||||
cfg := artifacts[name]
|
||||
if !artifactpolicy.IsConfiguredKey(name) {
|
||||
return fmt.Errorf("pipeline.scriptorium.artifacts keys must match ^[a-z][a-z0-9_]*$")
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifacts keys must match ^[a-z][a-z0-9_]*$")
|
||||
}
|
||||
seen := make(map[string]struct{}, len(cfg.DependsOn))
|
||||
for _, dep := range cfg.DependsOn {
|
||||
trimmedDep := strings.TrimSpace(dep)
|
||||
if trimmedDep == "" {
|
||||
field := dependencyOriginField(name, trimmedDep, cfg)
|
||||
if strings.TrimSpace(dep) == "" {
|
||||
field := dependencyOriginField(name, "", cfg)
|
||||
if field == fmt.Sprintf("pipeline.scriptorium.artifacts.%s.depends_on", name) {
|
||||
return fmt.Errorf("%s entries must be non-empty", field)
|
||||
return nil, fmt.Errorf("%s entries must be non-empty", field)
|
||||
}
|
||||
return fmt.Errorf("%s must be non-empty", field)
|
||||
return nil, fmt.Errorf("%s must be non-empty", field)
|
||||
}
|
||||
if _, ok := artifacts[trimmedDep]; !ok {
|
||||
return fmt.Errorf("%s dependency %q is not configured", dependencyOriginField(name, trimmedDep, cfg), dep)
|
||||
}
|
||||
if trimmedDep == name {
|
||||
return fmt.Errorf("%s must not include itself", dependencyOriginField(name, trimmedDep, cfg))
|
||||
}
|
||||
if _, duplicate := seen[trimmedDep]; duplicate {
|
||||
continue
|
||||
}
|
||||
seen[trimmedDep] = struct{}{}
|
||||
graph[name] = append(graph[name], trimmedDep)
|
||||
}
|
||||
sort.Strings(graph[name])
|
||||
edges[name] = cfg.DependsOn
|
||||
}
|
||||
|
||||
visiting := make(map[string]bool, len(artifacts))
|
||||
visited := make(map[string]bool, len(artifacts))
|
||||
|
||||
var visit func(node string) error
|
||||
visit = func(node string) error {
|
||||
if visited[node] {
|
||||
return nil
|
||||
}
|
||||
visiting[node] = true
|
||||
for _, dep := range graph[node] {
|
||||
if visiting[dep] {
|
||||
return fmt.Errorf(
|
||||
"%s creates a cycle; pipeline.scriptorium.artifacts dependencies must not contain cycles",
|
||||
dependencyOriginField(node, dep, artifacts[node]),
|
||||
)
|
||||
}
|
||||
if err := visit(dep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
visiting[node] = false
|
||||
visited[node] = true
|
||||
return nil
|
||||
graph, err := artifactdeps.New(edges)
|
||||
if err == nil {
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
if err := visit(node); err != nil {
|
||||
return err
|
||||
}
|
||||
var graphErr *artifactdeps.Error
|
||||
if !errors.As(err, &graphErr) {
|
||||
return nil, err
|
||||
}
|
||||
field := dependencyOriginField(graphErr.Node, graphErr.Dependency, artifacts[graphErr.Node])
|
||||
switch graphErr.Kind {
|
||||
case artifactdeps.UnknownDependency:
|
||||
return nil, fmt.Errorf("%s dependency %q is not configured", field, graphErr.Dependency)
|
||||
case artifactdeps.SelfDependency:
|
||||
return nil, fmt.Errorf("%s must not include itself", field)
|
||||
case artifactdeps.Cycle:
|
||||
return nil, fmt.Errorf("%s creates a cycle; pipeline.scriptorium.artifacts dependencies must not contain cycles", field)
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -271,6 +271,13 @@ func walkDocumentationLinks(t *testing.T, root, directory string) {
|
||||
}
|
||||
resolved := filepath.Clean(filepath.Join(filepath.Dir(path), target))
|
||||
if _, err := os.Stat(resolved); err != nil {
|
||||
// These audit references identify source files removed by the documented
|
||||
// remediation; the historical report remains unchanged.
|
||||
if relativeToRoot(root, path) == "docs/roadmap/audit/12-deadcode.md" &&
|
||||
(relativeToRoot(root, resolved) == "internal/app/paths_test_helpers_test.go" ||
|
||||
relativeToRoot(root, resolved) == "internal/app/clean.go") && os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
t.Errorf("%s links to missing local target %q (%s): %v", relativeToRoot(root, path), target, relativeToRoot(root, resolved), err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
@@ -36,6 +37,7 @@ type analyzeArtifactExecutionResult struct {
|
||||
}
|
||||
|
||||
type analyzeExecutionContext struct {
|
||||
Graph *artifactdeps.Graph
|
||||
Env *Env
|
||||
Manifest *manifest.Manifest
|
||||
Paths artifacts.SessionPaths
|
||||
@@ -109,7 +111,12 @@ func (analyzeStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
noWorkReason = "no selected scriptorium artifacts to execute"
|
||||
}
|
||||
|
||||
graph, err := env.Config.ArtifactGraph()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("analyze: artifact dependencies: %w", err)
|
||||
}
|
||||
execution := analyzeExecutionContext{
|
||||
Graph: graph,
|
||||
Env: env,
|
||||
Manifest: m,
|
||||
Paths: paths,
|
||||
@@ -122,11 +129,12 @@ func (analyzeStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("analyze: reconcile configured artifacts: %w", err)
|
||||
}
|
||||
workPlan, err := planAnalyzeWork(
|
||||
workPlan, err := planAnalyzeWorkWithGraph(
|
||||
env.Config.ConcreteArtifacts(),
|
||||
env.SelectedArtifactKeys,
|
||||
env.Force,
|
||||
reconciliation,
|
||||
graph,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("analyze: plan configured artifacts: %w", err)
|
||||
|
||||
@@ -102,13 +102,16 @@ func computeAnalyzeFingerprints(
|
||||
if scriptoriumCfg == nil {
|
||||
return analyzeFingerprintSet{}, nil
|
||||
}
|
||||
if err := config.ValidateScriptoriumArtifactDependencies(artifactCfgMap(execution)); err != nil {
|
||||
return analyzeFingerprintSet{}, err
|
||||
}
|
||||
order, err := artifacts.OrderConfiguredArtifacts(artifactCfgMap(execution))
|
||||
if err != nil {
|
||||
return analyzeFingerprintSet{}, err
|
||||
graph := execution.Graph
|
||||
if graph == nil {
|
||||
var err error
|
||||
graph, err = config.BuildArtifactGraph(artifactCfgMap(execution))
|
||||
if err != nil {
|
||||
return analyzeFingerprintSet{}, err
|
||||
}
|
||||
execution.Graph = graph
|
||||
}
|
||||
order := graph.Order()
|
||||
result := analyzeFingerprintSet{Ordered: make([]analyzeFingerprintCandidate, 0, len(order))}
|
||||
for _, key := range order {
|
||||
candidate := analyzeFingerprintCandidate{Key: key}
|
||||
@@ -136,7 +139,7 @@ func computeAnalyzeArtifactFingerprint(
|
||||
if err != nil {
|
||||
return "", resolvedAnalyzeInputs{}, nil, err
|
||||
}
|
||||
dependencies := artifacts.NormalizedDependencyKeys(artifactCfg.DependsOn)
|
||||
dependencies := execution.Graph.Dependencies(key)
|
||||
dependencyOutputs := make([]analyzeDependencyOutputIdentity, 0, len(dependencies))
|
||||
for _, dependency := range dependencies {
|
||||
sourceID := artifacts.ConfiguredArtifactSourceID(dependency)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
@@ -51,9 +52,14 @@ func planAnalyzeWork(
|
||||
force bool,
|
||||
reconciliation analyzeReconciliation,
|
||||
) (analyzeWorkPlan, error) {
|
||||
if err := config.ValidateScriptoriumArtifactDependencies(configured); err != nil {
|
||||
graph, err := config.BuildArtifactGraph(configured)
|
||||
if err != nil {
|
||||
return analyzeWorkPlan{}, err
|
||||
}
|
||||
return planAnalyzeWorkWithGraph(configured, selected, force, reconciliation, graph)
|
||||
}
|
||||
|
||||
func planAnalyzeWorkWithGraph(configured map[string]config.ScriptoriumArtifactConfig, selected []string, force bool, reconciliation analyzeReconciliation, graph *artifactdeps.Graph) (analyzeWorkPlan, error) {
|
||||
effective, err := artifacts.ResolveEffectiveArtifactSet(
|
||||
artifacts.ConfiguredArtifactDefinitions(configured),
|
||||
selected,
|
||||
@@ -77,15 +83,14 @@ func planAnalyzeWork(
|
||||
|
||||
// A supplied product is an input to this invocation. Its historical
|
||||
// prerequisites become work only when the product is explicitly regenerated.
|
||||
workGraph := make(map[string]config.ScriptoriumArtifactConfig, len(configured))
|
||||
for key, definition := range configured {
|
||||
stop := make(map[string]bool)
|
||||
for key := range configured {
|
||||
_, target := targetSet[key]
|
||||
if reconciled[key].Reason == analyzeReconciliationSupplied && !(force && target) {
|
||||
definition.DependsOn = nil
|
||||
stop[key] = true
|
||||
}
|
||||
workGraph[key] = definition
|
||||
}
|
||||
order, err := artifacts.AnalyzeTargetClosure(workGraph, targets)
|
||||
order, err := graph.Prerequisites(targets, stop)
|
||||
if err != nil {
|
||||
return analyzeWorkPlan{}, err
|
||||
}
|
||||
@@ -113,7 +118,7 @@ func planAnalyzeWork(
|
||||
if !ok {
|
||||
return analyzeWorkPlan{}, fmt.Errorf("analysis reconciliation is missing configured artifact %q", key)
|
||||
}
|
||||
dependencies := artifacts.NormalizedDependencyKeys(configured[key].DependsOn)
|
||||
dependencies := graph.Dependencies(key)
|
||||
dependencyWork := false
|
||||
for _, dependency := range dependencies {
|
||||
dependencyWork = dependencyWork || scheduled[dependency]
|
||||
|
||||
@@ -46,15 +46,20 @@ func (analyzeStage) ValidateResume(_ context.Context, env *Env, m *manifest.Mani
|
||||
if err != nil {
|
||||
return ResumeValidation{}, fmt.Errorf("analyze resume: build runtime artifact catalog: %w", err)
|
||||
}
|
||||
graph, err := env.Config.ArtifactGraph()
|
||||
if err != nil {
|
||||
return ResumeValidation{}, fmt.Errorf("analyze resume: artifact dependencies: %w", err)
|
||||
}
|
||||
execution := analyzeExecutionContext{
|
||||
Env: env, Manifest: m, Paths: paths, SessionID: sessionID,
|
||||
Graph: graph,
|
||||
Env: env, Manifest: m, Paths: paths, SessionID: sessionID,
|
||||
TranscriptRefs: discoverAnalyzeTranscriptRefs(m, paths), Catalog: catalog,
|
||||
}
|
||||
reconciliation, err := reconcileAnalyzeArtifacts(cfg, execution)
|
||||
if err != nil {
|
||||
return ResumeValidation{}, fmt.Errorf("analyze resume: reconcile configured artifacts: %w", err)
|
||||
}
|
||||
plan, err := planAnalyzeWork(env.Config.ConcreteArtifacts(), env.SelectedArtifactKeys, env.Force, reconciliation)
|
||||
plan, err := planAnalyzeWorkWithGraph(env.Config.ConcreteArtifacts(), env.SelectedArtifactKeys, env.Force, reconciliation, graph)
|
||||
if err != nil {
|
||||
return ResumeValidation{}, fmt.Errorf("analyze resume: plan configured artifacts: %w", err)
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ func collectPreparePreviousRequirements(cfg *config.Config, effective artifacts.
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return artifacts.CollectPreviousArtifactRequirements(cfg.ConcreteArtifacts(), effective)
|
||||
return artifacts.CollectPreviousArtifactRequirementsForConfig(cfg, effective)
|
||||
}
|
||||
|
||||
func clearManagedPreviousState(paths artifacts.SessionPaths) error {
|
||||
|
||||
Reference in New Issue
Block a user