Expose native session planning and artifact catalogs
This commit is contained in:
79
internal/app/artifact_catalog_command.go
Normal file
79
internal/app/artifact_catalog_command.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type artifactCatalogReport struct {
|
||||
Artifacts []artifactDefinitionReport `json:"artifacts"`
|
||||
}
|
||||
type artifactDefinitionReport struct {
|
||||
Key string `json:"key,omitempty"`
|
||||
Source string `json:"source"`
|
||||
Producer string `json:"producer"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
Dependencies []string `json:"dependencies"`
|
||||
Family string `json:"family,omitempty"`
|
||||
CharacterID string `json:"character_id,omitempty"`
|
||||
DefinitionSources []string `json:"definition_sources,omitempty"`
|
||||
FamilySources []string `json:"family_sources,omitempty"`
|
||||
PartySource string `json:"party_source,omitempty"`
|
||||
}
|
||||
|
||||
func artifactCatalogCommand(deps commandDependencies) commandkit.Command {
|
||||
common := newConfigurationFlags(func(input *configurationSelection) *configurationSelection { return input })
|
||||
return commandkit.NewCommand(commandkit.CommandSpec[configurationSelection]{
|
||||
Name: "list", Summary: "List artifact definitions for the selected pipeline and party.",
|
||||
Flags: []commandkit.Flag[configurationSelection]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.PartyFile, common.SecretsDir},
|
||||
CheckInput: checkConfigurationSelection,
|
||||
Run: func(invocation commandkit.Invocation, input configurationSelection) commandkit.Outcome {
|
||||
report, err := loadArtifactCatalogReport(invocation.Context, input, deps)
|
||||
if report != nil {
|
||||
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
||||
}
|
||||
return operationOutcome(err)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func loadArtifactCatalogReport(ctx context.Context, input configurationSelection, deps commandDependencies) (report *artifactCatalogReport, err error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
deps.secretsBootstrap = &secretsBootstrap{}
|
||||
acquired := newConfigurationAcquisition(input, deps)
|
||||
defer func() { err = errors.Join(err, acquired.Close()) }()
|
||||
catalog, err := acquired.Catalog(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
configured := artifacts.ConfiguredArtifactDefinitions(catalog.Artifacts)
|
||||
effective, err := artifacts.ResolveEffectiveArtifactSet(configured, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtime, err := artifacts.BootstrapRuntimeCatalog(configured, effective, artifacts.ExtractionDefinitionsFromConfig(acquired.pipeline.Notarius))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
report = &artifactCatalogReport{Artifacts: []artifactDefinitionReport{}}
|
||||
for _, spec := range artifacts.RuntimeTranscriptArtifacts() {
|
||||
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Source: spec.SourceID, Producer: spec.ProducerStage, Output: spec.CanonicalRelPath, Dependencies: []string{}})
|
||||
}
|
||||
if bounds, ok := runtime.Lookup(artifacts.ArtifactBoundsSession); ok {
|
||||
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Source: bounds.SourceID, Producer: bounds.ProducerStage, Output: bounds.CanonicalRelPath, Dependencies: []string{}})
|
||||
}
|
||||
for _, record := range catalog.Records {
|
||||
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Key: record.Key, Source: record.SourceID, Producer: "analyze", Output: record.OutputPath, Enabled: &record.Enabled, Dependencies: append([]string{}, record.Dependencies...), Family: record.FamilyKey, CharacterID: record.CharacterID, DefinitionSources: record.DefinitionSources, FamilySources: record.FamilySources, PartySource: record.PartySource})
|
||||
}
|
||||
for _, entry := range runtime.ListExtraction() {
|
||||
enabled := acquired.pipeline.Notarius != nil && acquired.pipeline.Notarius.Enabled
|
||||
report.Artifacts = append(report.Artifacts, artifactDefinitionReport{Key: entry.ExtractionKey, Source: entry.SourceID, Producer: entry.ProducerStage, Enabled: &enabled, Dependencies: []string{}})
|
||||
}
|
||||
sort.Slice(report.Artifacts, func(i, j int) bool { return report.Artifacts[i].Source < report.Artifacts[j].Source })
|
||||
return report, nil
|
||||
}
|
||||
@@ -18,8 +18,8 @@ func newApplication(deps commandDependencies, executor pipelineExecutor) (*comma
|
||||
commandkit.Identity{Name: "narratio", Version: buildinfo.Version},
|
||||
configurationCommand(deps),
|
||||
executionCommand("run", "Execute a session pipeline.", false, deps, executor),
|
||||
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts.", executionCommand("regenerate", "Rebuild extraction and analysis artifacts.", true, deps, executor)),
|
||||
commandkit.Group("session", "Manage and inspect sessions."),
|
||||
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts.", artifactCatalogCommand(deps), executionCommand("regenerate", "Rebuild extraction and analysis artifacts.", true, deps, executor)),
|
||||
commandkit.Group("session", "Manage and inspect sessions.", sessionPlanCommand(deps)),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,201 +3,105 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
const artifactCatalogHeader = "key\tsource\tdefinitions\tenabled\toutput\tdependencies\tfamily\tcharacter\tfamily_source\tparty_source\n"
|
||||
func catalogReportForTest(t *testing.T, args []string) *artifactCatalogReport {
|
||||
t.Helper()
|
||||
output := nativeInspectionForTest(t, append([]string{"artifacts", "list", "--format", "json"}, args...))
|
||||
var report artifactCatalogReport
|
||||
if err := json.Unmarshal([]byte(output), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &report
|
||||
}
|
||||
|
||||
func TestConfigArtifactsReportsOrdinaryCatalogWithoutCampaign(t *testing.T) {
|
||||
func TestArtifactCatalogCommandWithoutCampaignPreservesDependencies(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipelinePath, `workspace:
|
||||
pipeline := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipeline, `workspace:
|
||||
root: `+filepath.Join(dir, "workspace")+`
|
||||
whisperx:
|
||||
transcribe_url: https://transcription.example.com/transcribe
|
||||
whisperx: {transcribe_url: https://transcription.example.test/transcribe}
|
||||
scriptorium:
|
||||
binary: scriptorium
|
||||
artifacts:
|
||||
zeta:
|
||||
enabled: false
|
||||
output_path: artifacts/zeta.md
|
||||
zeta: {enabled: false, output_path: artifacts/zeta.md}
|
||||
alpha:
|
||||
enabled: false
|
||||
output_path: artifacts/alpha.md
|
||||
inputs:
|
||||
upstream: {source: narratio.artifact.zeta, required: true}
|
||||
`)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, &out); err != nil {
|
||||
t.Fatalf("ConfigArtifacts() error = %v", err)
|
||||
report := catalogReportForTest(t, []string{"--config", pipeline})
|
||||
found := map[string]artifactDefinitionReport{}
|
||||
for _, entry := range report.Artifacts {
|
||||
found[entry.Source] = entry
|
||||
}
|
||||
lines := strings.Split(strings.TrimSuffix(out.String(), "\n"), "\n")
|
||||
if len(lines) != 3 || !strings.HasPrefix(lines[1], "alpha\t") || !strings.HasPrefix(lines[2], "zeta\t") {
|
||||
t.Fatalf("catalog order/output = %q", out.String())
|
||||
alpha := found["narratio.artifact.alpha"]
|
||||
if alpha.Producer != "analyze" || alpha.Enabled == nil || *alpha.Enabled || !reflect.DeepEqual(alpha.Dependencies, []string{"zeta"}) || !reflect.DeepEqual(alpha.DefinitionSources, []string{pipeline}) {
|
||||
t.Fatalf("alpha=%#v", alpha)
|
||||
}
|
||||
alpha := strings.Split(lines[1], "\t")
|
||||
if len(alpha) != 10 {
|
||||
t.Fatalf("alpha fields = %#v", alpha)
|
||||
if found["narratio.transcript.base"].Producer != "merge" {
|
||||
t.Fatal("catalog omitted built-in producer")
|
||||
}
|
||||
if alpha[1] != "narratio.artifact.alpha" || alpha[2] != pipelinePath || alpha[3] != "false" || alpha[4] != "artifacts/alpha.md" || alpha[5] != "zeta" {
|
||||
t.Fatalf("alpha record = %#v", alpha)
|
||||
if _, err := os.Stat(filepath.Join(dir, "workspace")); !os.IsNotExist(err) {
|
||||
t.Fatalf("listing created workspace: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigArtifactsEmptyCatalogWritesOnlyHeader(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipelinePath, "workspace:\n root: "+filepath.Join(dir, "workspace")+"\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\n")
|
||||
var out bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, &out); err != nil {
|
||||
func TestArtifactCatalogCommandUsesExplicitRosterAndNativeConfiguration(t *testing.T) {
|
||||
pipeline, _ := writeInspectionFamilyConfig(t)
|
||||
overlay := filepath.Join(filepath.Dir(pipeline), "overlays", "testing.yml")
|
||||
mustWriteTestFile(t, overlay, "scriptorium:\n artifact_families:\n character_note:\n prompt_id: dnd.character_note.testing\n")
|
||||
roster := filepath.Join(t.TempDir(), "selected.yml")
|
||||
mustWriteTestFile(t, roster, "schema_version: narratio.party.v1\ncharacters:\n synthetic_character:\n player: {name: Synthetic Player}\n character: {name: Synthetic Character, classes: [{name: ranger}]}\n")
|
||||
selected := "testing"
|
||||
loaded, err := config.LoadPipelineWithOptions(pipeline, config.PipelineLoadOptions{Overlay: &selected})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out.String() != artifactCatalogHeader {
|
||||
t.Fatalf("empty catalog = %q, want header only", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigArtifactsReportsGeneratedFamilyProvenance(t *testing.T) {
|
||||
pipelinePath, campaignPath := writeInspectionFamilyConfig(t)
|
||||
var out bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath, "--campaign-file", campaignPath}, &out); err != nil {
|
||||
var before, after bytes.Buffer
|
||||
if err := loaded.ConfigKitResult().WriteEffective(&before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
records := catalogRecordsByKey(t, out.String())
|
||||
meta := records["character_meta_arannis"]
|
||||
note := records["character_note_arannis"]
|
||||
partyPath := filepath.Join(filepath.Dir(campaignPath), "party.yml")
|
||||
if len(meta) != 10 || meta[6] != "character_meta" || meta[7] != "arannis" || meta[8] != pipelinePath || meta[9] != partyPath {
|
||||
t.Fatalf("generated meta record = %#v", meta)
|
||||
}
|
||||
if len(note) != 10 || note[5] != "character_meta_arannis" || note[6] != "character_note" {
|
||||
t.Fatalf("generated note record = %#v", note)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigArtifactsReportsOverlayFamilyContributors(t *testing.T) {
|
||||
pipelinePath, campaignPath := writeInspectionFamilyConfig(t)
|
||||
overlayPath := filepath.Join(filepath.Dir(pipelinePath), "overlays", "testing.yml")
|
||||
mustWriteTestFile(t, overlayPath, `scriptorium:
|
||||
artifact_families:
|
||||
character_note:
|
||||
prompt_id: dnd.character_note.testing
|
||||
`)
|
||||
|
||||
args := []string{"--config", pipelinePath, "--campaign-file", campaignPath, "--overlay", "testing"}
|
||||
var catalog bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), args, &catalog); err != nil {
|
||||
app, err := newApplication(commandDependencies{loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) { return loaded, nil }}, executePlan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
records := catalogRecordsByKey(t, catalog.String())
|
||||
if got := strings.Split(records["character_note_arannis"][2], ","); !reflect.DeepEqual(got, []string{overlayPath, pipelinePath}) {
|
||||
t.Fatalf("generated family definitions = %#v", got)
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"artifacts", "list", "--config", pipeline, "--overlay", selected, "--party-file", roster, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 0 {
|
||||
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConfigArtifactsReportsBaseAndOverlayContributors(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
||||
overlayPath := filepath.Join(dir, "overlays", "testing.yml")
|
||||
mustWriteTestFile(t, pipelinePath, `workspace:
|
||||
root: /srv/narratio
|
||||
whisperx:
|
||||
transcribe_url: https://transcription.example.com/transcribe
|
||||
scriptorium:
|
||||
binary: scriptorium
|
||||
artifacts:
|
||||
notes:
|
||||
enabled: false
|
||||
output_path: artifacts/notes.md
|
||||
`)
|
||||
mustWriteTestFile(t, overlayPath, `scriptorium:
|
||||
artifacts:
|
||||
notes:
|
||||
enabled: true
|
||||
prompt_id: notes
|
||||
overlay_only:
|
||||
enabled: false
|
||||
output_path: artifacts/overlay-only.md
|
||||
`)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath, "--overlay", "testing"}, &out); err != nil {
|
||||
var report artifactCatalogReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
records := catalogRecordsByKey(t, out.String())
|
||||
if got := strings.Split(records["notes"][2], ","); !reflect.DeepEqual(got, []string{overlayPath, pipelinePath}) {
|
||||
t.Fatalf("notes definitions = %#v", got)
|
||||
}
|
||||
if got := records["overlay_only"][2]; got != overlayPath {
|
||||
t.Fatalf("overlay-only definition = %q, want %q", got, overlayPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigArtifactsIdentityDoesNotDependOnDefinitionFilename(t *testing.T) {
|
||||
inlineDir := t.TempDir()
|
||||
inlinePath := filepath.Join(inlineDir, "pipeline.yml")
|
||||
mustWriteTestFile(t, inlinePath, artifactIdentityPipeline("", "recap"))
|
||||
importDir := t.TempDir()
|
||||
importPath := filepath.Join(importDir, "pipeline.yml")
|
||||
mustWriteTestFile(t, importPath, artifactIdentityPipeline("artifacts.yml", ""))
|
||||
mustWriteTestFile(t, filepath.Join(importDir, "artifacts.yml"), "scriptorium:\n artifacts:\n recap:\n enabled: false\n output_path: artifacts/recap.md\n")
|
||||
|
||||
for _, path := range []string{inlinePath, importPath} {
|
||||
var out bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), []string{"--config", path}, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := catalogRecordsByKey(t, out.String())["recap"][1]; got != "narratio.artifact.recap" {
|
||||
t.Fatalf("source identity for %s = %q", path, got)
|
||||
found := false
|
||||
for _, entry := range report.Artifacts {
|
||||
if entry.Family == "character_note" {
|
||||
if entry.CharacterID != "synthetic_character" || entry.PartySource != roster || entry.Key != "character_note_synthetic_character" {
|
||||
t.Fatalf("member=%#v", entry)
|
||||
}
|
||||
if !reflect.DeepEqual(entry.DefinitionSources, []string{overlay, pipeline}) {
|
||||
t.Fatalf("definition sources=%v", entry.DefinitionSources)
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigArtifactsPropagatesWriterErrorsAndEscapesFields(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipelinePath, "workspace:\n root: /srv/narratio\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\n")
|
||||
want := errors.New("write rejected")
|
||||
err := ConfigArtifacts(context.Background(), []string{"--config", pipelinePath}, rejectingCatalogWriter{err: want})
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("ConfigArtifacts() error = %v, want writer error", err)
|
||||
if !found {
|
||||
t.Fatal("catalog omitted selected family member")
|
||||
}
|
||||
if got := escapeCatalogField("a\\b\tc\nd\r"); got != `a\\b\tc\nd\r` {
|
||||
t.Fatalf("escaped field = %q", got)
|
||||
if err := loaded.ConfigKitResult().WriteEffective(&after); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(before.Bytes(), after.Bytes()) {
|
||||
t.Fatal("catalog derivation changed native configuration")
|
||||
}
|
||||
}
|
||||
|
||||
func catalogRecordsByKey(t *testing.T, output string) map[string][]string {
|
||||
t.Helper()
|
||||
lines := strings.Split(strings.TrimSuffix(output, "\n"), "\n")
|
||||
if len(lines) == 0 || lines[0]+"\n" != artifactCatalogHeader {
|
||||
t.Fatalf("catalog header = %q", output)
|
||||
}
|
||||
records := make(map[string][]string, len(lines)-1)
|
||||
for _, line := range lines[1:] {
|
||||
fields := strings.Split(line, "\t")
|
||||
records[fields[0]] = fields
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func artifactIdentityPipeline(importPath, inlineKey string) string {
|
||||
document := "workspace:\n root: /srv/narratio\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\nscriptorium:\n binary: scriptorium\n"
|
||||
if importPath != "" {
|
||||
document = "config:\n imports:\n - path: " + importPath + "\n" + document
|
||||
}
|
||||
if inlineKey != "" {
|
||||
document += " artifacts:\n " + inlineKey + ":\n enabled: false\n output_path: artifacts/recap.md\n"
|
||||
}
|
||||
return document
|
||||
}
|
||||
|
||||
type rejectingCatalogWriter struct{ err error }
|
||||
|
||||
func (w rejectingCatalogWriter) Write([]byte) (int, error) { return 0, w.err }
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
)
|
||||
|
||||
type inspectionFlags struct {
|
||||
pipelinePath string
|
||||
campaignPath string
|
||||
campaignFilePath string
|
||||
overlay singletonStringFlag
|
||||
secretsDir singletonStringFlag
|
||||
}
|
||||
|
||||
type inspectionConfig struct {
|
||||
PipelinePath string
|
||||
Pipeline *config.PipelineConfig
|
||||
CampaignPath string
|
||||
Campaign *config.CampaignConfig
|
||||
Party config.ResolvedParty
|
||||
ArtifactCatalog *config.ArtifactCatalog
|
||||
}
|
||||
|
||||
// ConfigArtifacts validates and reports the effective concrete artifact catalog
|
||||
// without loading session state or runtime collaborators.
|
||||
func ConfigArtifacts(ctx context.Context, args []string, out io.Writer) error {
|
||||
return configArtifactsWithDependencies(ctx, args, out, commandDependencies{})
|
||||
}
|
||||
|
||||
func configArtifactsWithDependencies(_ context.Context, args []string, out io.Writer, deps commandDependencies) error {
|
||||
flags, err := parseInspectionFlags("config artifacts", args, out)
|
||||
if err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
resolved, err := resolveInspectionConfigWithDependencies(flags, deps)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config artifacts: %w", err)
|
||||
}
|
||||
records := resolved.ArtifactCatalog.Records
|
||||
if _, err := fmt.Fprintln(out, "key\tsource\tdefinitions\tenabled\toutput\tdependencies\tfamily\tcharacter\tfamily_source\tparty_source"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, record := range records {
|
||||
fields := []string{
|
||||
record.Key,
|
||||
record.SourceID,
|
||||
strings.Join(normalizedCatalogPaths(record.DefinitionSources), ","),
|
||||
strconv.FormatBool(record.Enabled),
|
||||
record.OutputPath,
|
||||
strings.Join(record.Dependencies, ","),
|
||||
record.FamilyKey,
|
||||
record.CharacterID,
|
||||
strings.Join(normalizedCatalogPaths(record.FamilySources), ","),
|
||||
config.NormalizedConfigurationPath(record.PartySource),
|
||||
}
|
||||
for index := range fields {
|
||||
fields[index] = escapeCatalogField(fields[index])
|
||||
}
|
||||
if _, err := fmt.Fprintln(out, strings.Join(fields, "\t")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizedCatalogPaths(paths []string) []string {
|
||||
result := make([]string, len(paths))
|
||||
for index, path := range paths {
|
||||
result[index] = config.NormalizedConfigurationPath(path)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
func escapeCatalogField(value string) string {
|
||||
value = strings.ReplaceAll(value, "\\", "\\\\")
|
||||
value = strings.ReplaceAll(value, "\t", "\\t")
|
||||
value = strings.ReplaceAll(value, "\r", "\\r")
|
||||
return strings.ReplaceAll(value, "\n", "\\n")
|
||||
}
|
||||
|
||||
func parseInspectionFlags(command string, args []string, out io.Writer) (inspectionFlags, error) {
|
||||
fs := flag.NewFlagSet(command, flag.ContinueOnError)
|
||||
fs.SetOutput(out)
|
||||
var flags inspectionFlags
|
||||
fs.StringVar(&flags.pipelinePath, "config", "", "path to pipeline.yml (optional; defaults searched)")
|
||||
fs.StringVar(&flags.campaignPath, "campaign", "", "campaign ID")
|
||||
fs.StringVar(&flags.campaignFilePath, "campaign-file", "", "path to campaign.yml")
|
||||
flags.overlay.name = "overlay"
|
||||
fs.Var(&flags.overlay, "overlay", "named pipeline overlay")
|
||||
flags.secretsDir.name = "secrets-dir"
|
||||
fs.Var(&flags.secretsDir, "secrets-dir", "secrets directory (optional; defaults to /usr/local/etc/narratio/secrets)")
|
||||
fs.Usage = func() {
|
||||
fmt.Fprintf(out, "Usage: narratio %s [--config <pipeline.yml>] [--campaign <id> | --campaign-file <campaign.yml>] [--overlay <name>] [--secrets-dir <path>]\n\n", command)
|
||||
fmt.Fprintln(out, "Resolves configuration without a session, workspace, manifest, adapters, or external services.")
|
||||
fmt.Fprintln(out)
|
||||
fmt.Fprintln(out, "Flags:")
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return inspectionFlags{}, fmt.Errorf("%s: invalid flags: %w", command, err)
|
||||
}
|
||||
if fs.NArg() != 0 {
|
||||
return inspectionFlags{}, fmt.Errorf("%s: unexpected positional arguments", command)
|
||||
}
|
||||
if strings.TrimSpace(flags.campaignPath) != "" && strings.TrimSpace(flags.campaignFilePath) != "" {
|
||||
return inspectionFlags{}, fmt.Errorf("%s: --campaign and --campaign-file are mutually exclusive", command)
|
||||
}
|
||||
return flags, nil
|
||||
}
|
||||
|
||||
func resolveInspectionConfigWithDependencies(flags inspectionFlags, deps commandDependencies) (*inspectionConfig, error) {
|
||||
pipelinePath, pipeline, err := loadPipelineConfigWithDependencies(flags.pipelinePath, config.PipelineLoadOptions{Overlay: flags.overlay.pointer()}, flags.secretsDir.pointer(), deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
needsCampaign := pipelineHasArtifactFamilies(pipeline)
|
||||
hasCampaignSelection := strings.TrimSpace(flags.campaignPath) != "" || strings.TrimSpace(flags.campaignFilePath) != ""
|
||||
if !needsCampaign && !hasCampaignSelection {
|
||||
if err := validateInspectionPipeline(pipelinePath, pipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
catalog, err := config.DeriveArtifactCatalog(pipeline, config.ResolvedParty{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &inspectionConfig{PipelinePath: pipelinePath, Pipeline: pipeline, ArtifactCatalog: catalog}, nil
|
||||
}
|
||||
|
||||
campaignPath, err := resolveCampaignConfigPath(pipeline, flags.campaignPath, flags.campaignFilePath)
|
||||
if err != nil {
|
||||
if needsCampaign {
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifact_families requires a campaign: %w", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
campaign, err := config.LoadCampaign(campaignPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if selectedID := strings.TrimSpace(flags.campaignPath); selectedID != "" && strings.TrimSpace(flags.campaignFilePath) == "" {
|
||||
if got := config.CampaignID(campaign); got != selectedID {
|
||||
return nil, fmt.Errorf("campaign config %q invalid: campaign_id %q does not match selected campaign %q", campaignPath, got, selectedID)
|
||||
}
|
||||
}
|
||||
loaded, err := config.LoadPipelineCampaign(pipelinePath, pipeline, campaignPath, campaign)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionPipeline(loaded.PipelinePath, loaded.Pipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionCampaign(loaded.CampaignPath, loaded.Campaign); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &inspectionConfig{
|
||||
PipelinePath: loaded.PipelinePath,
|
||||
Pipeline: loaded.Pipeline,
|
||||
CampaignPath: loaded.CampaignPath,
|
||||
Campaign: loaded.Campaign,
|
||||
Party: loaded.Party,
|
||||
ArtifactCatalog: loaded.ArtifactCatalog,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func pipelineHasArtifactFamilies(pipeline *config.PipelineConfig) bool {
|
||||
return pipeline != nil && pipeline.Scriptorium != nil && len(pipeline.Scriptorium.ArtifactFamilies) != 0
|
||||
}
|
||||
|
||||
func validateInspectionPipeline(path string, pipeline *config.PipelineConfig) error {
|
||||
if err := config.ValidatePipelineConfig(pipeline); err != nil {
|
||||
return fmt.Errorf("pipeline config %q invalid: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateInspectionCampaign(path string, campaign *config.CampaignConfig) error {
|
||||
if err := config.ValidateCampaignConfig(campaign); err != nil {
|
||||
return fmt.Errorf("campaign config %q invalid: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -147,11 +147,8 @@ func TestNativeBundleInspectionAndExecutionAgree(t *testing.T) {
|
||||
t.Fatal("native show omitted authored families")
|
||||
}
|
||||
}
|
||||
var catalog bytes.Buffer
|
||||
if err := ConfigArtifacts(context.Background(), common, &catalog); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(catalog.String(), "character_meta_arannis") {
|
||||
catalog := nativeInspectionForTest(t, append([]string{"artifacts", "list", "--format", "json"}, common...))
|
||||
if !strings.Contains(catalog, "character_meta_arannis") {
|
||||
t.Fatal("domain catalog omitted concrete family member")
|
||||
}
|
||||
diff := nativeInspectionForTest(t, []string{"config", "diff", "--details", "--against-overlay", "testing", "--secrets-dir", secrets, "--format", "json"})
|
||||
|
||||
@@ -25,6 +25,7 @@ type configurationAcquisition struct {
|
||||
session *acquiredSession
|
||||
previousSessionID string
|
||||
domain *config.Config
|
||||
catalog *config.ArtifactCatalog
|
||||
pipelineOverrides []configkit.Override
|
||||
}
|
||||
|
||||
@@ -239,10 +240,11 @@ func (a *configurationAcquisition) Domain(ctx context.Context) (*config.Config,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base, err := config.LoadPipelineCampaignWithParty(a.pipelinePath, pipeline, a.campaignPath, campaign, *party)
|
||||
catalog, err := a.Catalog(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := config.LoadedPipelineCampaign{PipelinePath: a.pipelinePath, Pipeline: pipeline, CampaignPath: a.campaignPath, Campaign: campaign, Party: *party, ArtifactCatalog: catalog}
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(base, a.session.Path, session, a.session.Source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -259,3 +261,27 @@ func (a *configurationAcquisition) Close() error {
|
||||
a.session.cleanup = nil
|
||||
return cleanup()
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Catalog(ctx context.Context) (*config.ArtifactCatalog, error) {
|
||||
pipeline, err := a.Pipeline(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.catalog != nil {
|
||||
return a.catalog, nil
|
||||
}
|
||||
var party config.ResolvedParty
|
||||
if a.party != nil || a.selection.PartyFile.Set || a.selection.Campaign.Set || a.selection.CampaignFile.Set || (pipeline.Scriptorium != nil && len(pipeline.Scriptorium.ArtifactFamilies) != 0) {
|
||||
selected, err := a.Party(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
party = *selected
|
||||
}
|
||||
catalog, err := config.DeriveArtifactCatalog(pipeline, party)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.catalog = catalog
|
||||
return catalog, nil
|
||||
}
|
||||
|
||||
@@ -65,6 +65,18 @@ func checkExecutionInput(input *executionInput) error {
|
||||
}
|
||||
|
||||
func executionCommand(name, summary string, regenerate bool, deps commandDependencies, executor pipelineExecutor) commandkit.Command {
|
||||
spec := executionCommandSpec(name, summary, regenerate)
|
||||
spec.Run = func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
|
||||
report, err := executeSelectedPipeline(invocation.Context, input, deps, executor)
|
||||
if report != nil {
|
||||
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
||||
}
|
||||
return operationOutcome(err)
|
||||
}
|
||||
return commandkit.NewCommand(spec)
|
||||
}
|
||||
|
||||
func executionCommandSpec(name, summary string, regenerate bool) commandkit.CommandSpec[*executionInput] {
|
||||
common := newConfigurationFlags(func(input **executionInput) *configurationSelection { return &(*input).Selection })
|
||||
flags := []commandkit.Flag[*executionInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile, common.SecretsDir,
|
||||
commandkit.ValueFlag(commandkit.FlagMeta{Name: "previous-session-id", Usage: "Assert the previous session identity."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).PreviousSession = value }),
|
||||
@@ -78,7 +90,7 @@ func executionCommand(name, summary string, regenerate bool, deps commandDepende
|
||||
commandkit.BoolFlag(commandkit.FlagMeta{Name: "force", Usage: "Rerun selected stages."}, false, func(input **executionInput, value commandkit.Supplied[bool]) { (*input).Force = value }),
|
||||
)
|
||||
}
|
||||
return commandkit.NewCommand(commandkit.CommandSpec[*executionInput]{
|
||||
return commandkit.CommandSpec[*executionInput]{
|
||||
Name: name, Summary: summary, NewInput: func() *executionInput { return &executionInput{} }, Flags: flags,
|
||||
Arguments: []commandkit.Argument[*executionInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SESSION", Usage: "Session identity."}, true, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Selection.Session = value })},
|
||||
CheckInput: func(input *executionInput) error {
|
||||
@@ -89,14 +101,7 @@ func executionCommand(name, summary string, regenerate bool, deps commandDepende
|
||||
}
|
||||
return checkExecutionInput(input)
|
||||
},
|
||||
Run: func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
|
||||
report, err := executeSelectedPipeline(invocation.Context, input, deps, executor)
|
||||
if report != nil {
|
||||
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
||||
}
|
||||
return operationOutcome(err)
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func executeSelectedPipeline(ctx context.Context, input *executionInput, deps commandDependencies, executor pipelineExecutor) (report *runReport, err error) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
// Plan validates configuration and prints a read-only execution preview.
|
||||
@@ -48,83 +49,33 @@ func planWithDependencies(ctx context.Context, args []string, out io.Writer, dep
|
||||
if err != nil {
|
||||
return fmt.Errorf("plan: %w", err)
|
||||
}
|
||||
m, err := loadManifestIfPresent(ctx, cfg)
|
||||
input := &executionInput{plan: request.Plan, Force: commandkit.Supplied[bool]{Value: request.Force}}
|
||||
report, err := buildSessionPlan(ctx, cfg, input, selectedArtifacts, effective)
|
||||
if err != nil {
|
||||
return fmt.Errorf("plan: %w", err)
|
||||
return err
|
||||
}
|
||||
if err := validateBoundedPrerequisites(request.Plan, m); err != nil {
|
||||
return fmt.Errorf("plan: %w", err)
|
||||
}
|
||||
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
||||
paths := store.SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
model, err := cloneManifestForPlan(m, cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("plan: clone session state: %w", err)
|
||||
}
|
||||
|
||||
stages := request.Plan.Stages()
|
||||
stageEnv := &stage.Env{
|
||||
Config: cfg, SelectedArtifactKeys: append([]string(nil), selectedArtifacts...),
|
||||
EffectiveArtifacts: effective, ArtifactStore: store, Force: request.Force,
|
||||
}
|
||||
|
||||
runCount := 0
|
||||
skipCount := 0
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, err := fmt.Fprintf(out, "narratio session plan: read-only workdir at %s; %s\n", paths.Root, effectiveConfigSummary(cfg)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, selectedStage := range stages {
|
||||
semanticConfig, err := currentStageSemanticConfig(selectedStage, stageEnv)
|
||||
if err != nil {
|
||||
return fmt.Errorf("plan: fingerprint semantic configuration for stage %q: %w", selectedStage.Name(), err)
|
||||
}
|
||||
decision := decideStageExecution(recordedStageStatus(model, selectedStage.Name()), request.Force, nil)
|
||||
var validation *stage.ResumeValidation
|
||||
if decision.action == stageActionSkip {
|
||||
checked, validationErr := evaluateStageResume(ctx, selectedStage, stageEnv, model, semanticConfig)
|
||||
if validationErr != nil {
|
||||
return fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
||||
}
|
||||
validation = checked
|
||||
decision = decideStageExecution(recordedStageStatus(model, selectedStage.Name()), request.Force, checked)
|
||||
}
|
||||
if _, err := applyPreExecutionDecision(model, selectedStage.Name(), decision, time.Now().UTC()); err != nil {
|
||||
return 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 fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
||||
}
|
||||
checked = checked.Normalized()
|
||||
validation = &checked
|
||||
}
|
||||
}
|
||||
if decision.action == stageActionRun {
|
||||
runCount, skipCount := 0, 0
|
||||
for _, entry := range report.Stages {
|
||||
if entry.Action == string(stageActionRun) {
|
||||
runCount++
|
||||
} else {
|
||||
skipCount++
|
||||
}
|
||||
if _, err := fmt.Fprintf(out, "%s: %s\n", selectedStage.Name(), decision.action); err != nil {
|
||||
if _, err := fmt.Fprintf(out, "%s: %s\n", entry.Name, entry.Action); err != nil {
|
||||
return err
|
||||
}
|
||||
if validation != nil && validation.Analyze != nil {
|
||||
if err := writeAnalyzePlanDetails(out, validation.Analyze); err != nil {
|
||||
if entry.Analysis != nil {
|
||||
if err := writeAnalyzePlanDetails(out, entry.Analysis); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if decision.action == stageActionRun {
|
||||
if err := modelPlannedStageRun(model, selectedStage, cfg, decision, semanticConfig); err != nil {
|
||||
return fmt.Errorf("plan: model stage %q: %w", selectedStage.Name(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := fmt.Fprintf(out, "totals: run=%d skip=%d\n", runCount, skipCount); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
_, err = fmt.Fprintf(out, "totals: run=%d skip=%d\n", runCount, skipCount)
|
||||
return err
|
||||
}
|
||||
|
||||
func cloneManifestForPlan(source *manifest.Manifest, cfg *config.Config) (*manifest.Manifest, error) {
|
||||
@@ -175,20 +126,20 @@ func plannedSelfSkipReason(stageName string, cfg *config.Config) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func writeAnalyzePlanDetails(out io.Writer, summary *stage.AnalyzeResumeSummary) error {
|
||||
func writeAnalyzePlanDetails(out io.Writer, summary *plannedAnalysis) error {
|
||||
if summary == nil {
|
||||
return nil
|
||||
}
|
||||
if _, err := fmt.Fprintf(out, " targets: %s\n", planStringList(summary.ExplicitTargets)); err != nil {
|
||||
if _, err := fmt.Fprintf(out, " targets: %s\n", planStringList(summary.Targets)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintf(out, " prerequisites: %s\n", planArtifactList(summary.PrerequisiteWork)); err != nil {
|
||||
if _, err := fmt.Fprintf(out, " prerequisites: %s\n", planArtifactList(summary.Prerequisites)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintf(out, " execute: %s\n", planArtifactList(summary.ExecutionOrder)); err != nil {
|
||||
if _, err := fmt.Fprintf(out, " execute: %s\n", planArtifactList(summary.Execute)); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := fmt.Fprintf(out, " reuse: %s\n", planArtifactList(summary.ReusedCurrent))
|
||||
_, err := fmt.Fprintf(out, " reuse: %s\n", planArtifactList(summary.Reuse))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -199,7 +150,7 @@ func planStringList(values []string) string {
|
||||
return strings.Join(values, ", ")
|
||||
}
|
||||
|
||||
func planArtifactList(values []stage.AnalyzeResumeArtifact) string {
|
||||
func planArtifactList(values []plannedArtifact) string {
|
||||
if len(values) == 0 {
|
||||
return "none"
|
||||
}
|
||||
|
||||
180
internal/app/plan_command.go
Normal file
180
internal/app/plan_command.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type sessionPlanReport struct {
|
||||
Campaign string `json:"campaign"`
|
||||
SessionID string `json:"session_id"`
|
||||
Stages []plannedStage `json:"stages"`
|
||||
Publication []plannedPublication `json:"publication_actions"`
|
||||
}
|
||||
type plannedStage struct {
|
||||
Name string `json:"name"`
|
||||
Action string `json:"action"`
|
||||
Reason string `json:"reason"`
|
||||
Analysis *plannedAnalysis `json:"analysis,omitempty"`
|
||||
}
|
||||
type plannedAnalysis struct {
|
||||
Targets []string `json:"targets"`
|
||||
Prerequisites []plannedArtifact `json:"prerequisites"`
|
||||
Execute []plannedArtifact `json:"execute"`
|
||||
Reuse []plannedArtifact `json:"reuse"`
|
||||
}
|
||||
type plannedArtifact struct {
|
||||
Key string `json:"key"`
|
||||
Family string `json:"family,omitempty"`
|
||||
CharacterID string `json:"character_id,omitempty"`
|
||||
Role string `json:"role"`
|
||||
Reason string `json:"reason"`
|
||||
Forced bool `json:"forced"`
|
||||
}
|
||||
type plannedPublication struct {
|
||||
Source string `json:"source"`
|
||||
Destination string `json:"destination"`
|
||||
Required bool `json:"required"`
|
||||
Selected bool `json:"selected"`
|
||||
}
|
||||
|
||||
func analysisPlanReport(summary *stage.AnalyzeResumeSummary) *plannedAnalysis {
|
||||
convert := func(items []stage.AnalyzeResumeArtifact) []plannedArtifact {
|
||||
out := make([]plannedArtifact, 0, len(items))
|
||||
for _, item := range items {
|
||||
out = append(out, plannedArtifact{Key: item.Key, Family: item.Family, CharacterID: item.CharacterID, Role: item.Role, Reason: item.Reason, Forced: item.Forced})
|
||||
}
|
||||
return out
|
||||
}
|
||||
return &plannedAnalysis{Targets: append([]string(nil), summary.ExplicitTargets...), Prerequisites: convert(summary.PrerequisiteWork), Execute: convert(summary.ExecutionOrder), Reuse: convert(summary.ReusedCurrent)}
|
||||
}
|
||||
|
||||
func sessionPlanCommand(deps commandDependencies) commandkit.Command {
|
||||
spec := executionCommandSpec("plan", "Preview session work without executing it.", false)
|
||||
spec.Run = func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
|
||||
report, err := planSelectedPipeline(invocation.Context, input, deps)
|
||||
if report != nil {
|
||||
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
||||
}
|
||||
return operationOutcome(err)
|
||||
}
|
||||
return commandkit.NewCommand(spec)
|
||||
}
|
||||
|
||||
func planSelectedPipeline(ctx context.Context, input *executionInput, deps commandDependencies) (report *sessionPlanReport, err error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
deps.secretsBootstrap = &secretsBootstrap{}
|
||||
acquired := newConfigurationAcquisition(input.Selection, deps)
|
||||
acquired.previousSessionID = input.PreviousSession.Value
|
||||
defer func() { err = errors.Join(err, acquired.Close()) }()
|
||||
cfg, err := acquired.Domain(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := config.Validate(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
selected, err := normalizeArtifactSelection(cfg, input.selectedArtifacts)
|
||||
if err != nil {
|
||||
return nil, &inputError{message: err.Error(), cause: err}
|
||||
}
|
||||
effective, err := resolveEffectiveArtifacts(cfg, selected)
|
||||
if err != nil {
|
||||
return nil, &inputError{message: err.Error(), cause: err}
|
||||
}
|
||||
return buildSessionPlan(ctx, cfg, input, selected, effective)
|
||||
}
|
||||
|
||||
func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionInput, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet) (*sessionPlanReport, error) {
|
||||
m, err := loadManifestIfPresent(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("plan: %w", err)
|
||||
}
|
||||
if err := validateBoundedPrerequisites(input.plan, m); err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
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 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 {
|
||||
return nil, err
|
||||
}
|
||||
actions, err := stage.PlanPublicationOutputs(catalog, cfg.PublicationOutputs(), selectedArtifacts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, action := range actions {
|
||||
report.Publication = append(report.Publication, plannedPublication{Source: action.Source, Destination: action.Destination, Required: action.Required, Selected: action.Selected})
|
||||
}
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
70
internal/app/plan_command_test.go
Normal file
70
internal/app/plan_command_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestSessionPlanCommandReportsWorkWithoutRuntimeState(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)
|
||||
}
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, err := os.Stat(paths.Root); !os.IsNotExist(err) {
|
||||
t.Fatalf("fixture session root=%v", err)
|
||||
}
|
||||
app, err := newApplication(commandDependencies{}, func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
|
||||
t.Fatal("planning executed pipeline")
|
||||
return nil, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--force", "--artifacts", "session_recap", "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 0 {
|
||||
t.Fatalf("status=%d error=%v diagnostics=%s", outcome.Status, outcome.Cause, diagnostic.String())
|
||||
}
|
||||
var report sessionPlanReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.SessionID != "2026-05-03" || len(report.Stages) != len(BuildFullPlan()) {
|
||||
t.Fatalf("plan=%#v", report)
|
||||
}
|
||||
foundAnalysis := false
|
||||
for _, entry := range report.Stages {
|
||||
if entry.Action != "run" || entry.Reason != "forced" {
|
||||
t.Fatalf("stage=%#v", entry)
|
||||
}
|
||||
if entry.Name == "analyze" {
|
||||
foundAnalysis = entry.Analysis != nil && len(entry.Analysis.Targets) > 0
|
||||
}
|
||||
}
|
||||
if !foundAnalysis {
|
||||
t.Fatal("plan omitted artifact work")
|
||||
}
|
||||
if len(report.Publication) != len(cfg.PublicationOutputs()) {
|
||||
t.Fatalf("publication actions=%#v", report.Publication)
|
||||
}
|
||||
for _, action := range report.Publication {
|
||||
if action.Destination == "" {
|
||||
t.Fatalf("unresolved publication action=%#v", action)
|
||||
}
|
||||
if action.Source == "narratio.artifact.player_handout" && action.Selected {
|
||||
t.Fatal("plan selected an unrequested publication target")
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(paths.Root); !os.IsNotExist(err) {
|
||||
t.Fatalf("planning created runtime state: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user