Finish native completion and retire legacy command machinery

This commit is contained in:
2026-11-20 06:32:47 +00:00
parent 7db626781e
commit faa8cb5822
72 changed files with 841 additions and 2966 deletions

View File

@@ -9,23 +9,6 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/config"
)
type artifactSelectionFlag struct {
values []string
}
func (f *artifactSelectionFlag) String() string {
return strings.Join(f.values, ",")
}
func (f *artifactSelectionFlag) Set(value string) error {
f.values = append(f.values, value)
return nil
}
func (f *artifactSelectionFlag) Normalize() ([]string, error) {
return normalizeArtifactNames(f.values)
}
func normalizeArtifactNames(values []string) ([]string, error) {
if len(values) == 0 {
return nil, nil
@@ -51,11 +34,6 @@ func normalizeArtifactNames(values []string) ([]string, error) {
return out, nil
}
func validateSelectedArtifacts(cfg *config.Config, selected []string) error {
_, err := resolveEffectiveArtifacts(cfg, selected)
return err
}
func resolveEffectiveArtifacts(cfg *config.Config, selected []string) (artifacts.EffectiveArtifactSet, error) {
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Scriptorium == nil {
if len(selected) == 0 {

View File

@@ -13,26 +13,26 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func TestExecuteRunStageArtifactsUnsupportedStageFails(t *testing.T) {
func TestExecuteRunSingleStageArtifactsUnsupportedStageFails(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(
[]string{"run-stage", "extract", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
[]string{"run", "2026-05-03", "--stage", "extract", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
&stdout,
&stderr,
)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `run-stage: --artifacts is only supported for stages "analyze" and "publish"`) {
if !strings.Contains(stderr.String(), `--artifacts requires a selected range containing analyze or publish`) {
t.Fatalf("stderr = %q, want stage-gating error", stderr.String())
}
}
func TestExecuteRunStagePublishPropagatesSelectedArtifacts(t *testing.T) {
func TestExecuteRunSingleStagePublishPropagatesSelectedArtifacts(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
@@ -49,11 +49,9 @@ func TestExecuteRunStagePublishPropagatesSelectedArtifacts(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithPipelineExecutor(
[]string{
"run-stage", "publish", "2026-05-03",
[]string{"run", "2026-05-03", "--stage", "publish",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--artifacts", "session_recap",
},
&stdout,
@@ -78,19 +76,19 @@ func TestExecuteUnknownArtifactsFailValidation(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(
[]string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "unknown_artifact"},
[]string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "unknown_artifact"},
&stdout,
&stderr,
)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `run: --artifacts includes unknown artifact "unknown_artifact"`) {
if !strings.Contains(stderr.String(), `--artifacts includes unknown artifact "unknown_artifact"`) {
t.Fatalf("stderr = %q, want unknown-artifact validation error", stderr.String())
}
}
func TestRunStageArtifactsDoesNotImplyForce(t *testing.T) {
func TestRunSingleStageArtifactsDoesNotImplyForce(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -102,16 +100,18 @@ func TestRunStageArtifactsDoesNotImplyForce(t *testing.T) {
t.Fatalf("save manifest: %v", err)
}
seedInvocationPrerequisites(t, workspaceRoot, "analyze")
var out bytes.Buffer
err := RunStage(
err := invokeNativeCommand(
context.Background(),
[]string{"analyze", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap,session_recap"},
[]string{"run", "--stage", "analyze", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap,session_recap"},
&out,
)
if err != nil {
t.Fatalf("RunStage() error = %v", err)
t.Fatalf("single-stage run error = %v", err)
}
if !strings.Contains(out.String(), "stage=analyze executed=1 skipped=0 force=false") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "analyze") {
t.Fatalf("output = %q, want legacy analyze evidence rebuilt without implying force", out.String())
}
}
@@ -122,9 +122,9 @@ func TestRunArtifactsWithSucceededAnalyzeSkipsUnlessForced(t *testing.T) {
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
store := &manifest.LocalStore{}
if err := RunStage(
if err := invokeNativeCommand(
context.Background(),
[]string{"prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
[]string{"run", "--stage", "prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
&bytes.Buffer{},
); err != nil {
t.Fatalf("seed prepare stage: %v", err)
@@ -143,15 +143,15 @@ func TestRunArtifactsWithSucceededAnalyzeSkipsUnlessForced(t *testing.T) {
}
var out bytes.Buffer
err = Run(
err = invokeNativeCommand(
context.Background(),
[]string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
[]string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
&out,
)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !strings.Contains(out.String(), "executed=5 skipped=9") {
if !runCountsMatch(t, out.Bytes(), 5, 9, "") {
t.Fatalf("output = %q, want extract reconsidered and legacy analyze plus delivery rebuilt", out.String())
}
}
@@ -176,7 +176,7 @@ func TestExecuteAnalyzeForceRunsAnalyze(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithPipelineExecutor(
[]string{"analyze", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
[]string{"run", "--stage", "analyze", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
&stdout,
&stderr,
executor,
@@ -190,7 +190,7 @@ func TestExecuteAnalyzeForceRunsAnalyze(t *testing.T) {
if !capturedForce {
t.Fatal("captured force = false, want true")
}
if !strings.Contains(stdout.String(), "narratio analyze: executed=1 skipped=0 force=true; manifest=") {
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "") {
t.Fatalf("stdout = %q, want analyze summary", stdout.String())
}
}
@@ -208,12 +208,9 @@ func TestExecuteAnalyzePropagatesSelectedArtifacts(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithPipelineExecutor(
[]string{
"analyze",
"2026-05-03",
[]string{"run", "--stage", "analyze", "--force", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--artifacts", "player_handout,session_recap",
},
&stdout,
@@ -235,47 +232,23 @@ func TestExecuteAnalyzeUnknownArtifactFailsValidation(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(
[]string{"analyze", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "unknown_artifact"},
[]string{"run", "--stage", "analyze", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "unknown_artifact"},
&stdout,
&stderr,
)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `analyze: --artifacts includes unknown artifact "unknown_artifact"`) {
if !strings.Contains(stderr.String(), `--artifacts includes unknown artifact "unknown_artifact"`) {
t.Fatalf("stderr = %q, want unknown-artifact validation error", stderr.String())
}
}
func TestExecuteAnalyzeRejectsPositionalArgsAndForceFlag(t *testing.T) {
cases := []struct {
name string
args []string
want string
}{
{name: "extra positional", args: []string{"analyze", "2026-05-03", "extra"}, want: "analyze: unexpected positional arguments"},
{name: "force flag", args: []string{"analyze", "--force"}, want: "analyze: invalid flags: flag provided but not defined: -force"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(tc.args, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), tc.want) {
t.Fatalf("stderr = %q, want %q", stderr.String(), tc.want)
}
})
}
}
func TestExecuteAnalyzeMissingConfigUsesRunStageLoadingPath(t *testing.T) {
func TestExecuteAnalyzeMissingConfigUsesSharedAcquisition(t *testing.T) {
deps, _, executor := guardedMissingPipelineDiscovery(t)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithDependencies([]string{"analyze", "2026-05-03"}, &stdout, &stderr, deps, executor)
code := executeWithDependencies([]string{"run", "2026-05-03", "--stage", "analyze", "--force"}, &stdout, &stderr, deps, executor)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
@@ -309,7 +282,7 @@ func TestExecutePublishForceRunsPublish(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithPipelineExecutor(
[]string{"publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
[]string{"run", "--stage", "publish", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
&stdout,
&stderr,
executor,
@@ -326,35 +299,11 @@ func TestExecutePublishForceRunsPublish(t *testing.T) {
if strings.Join(capturedArtifacts, ",") != "session_recap" {
t.Fatalf("captured artifacts = %#v, want [session_recap]", capturedArtifacts)
}
if !strings.Contains(stdout.String(), "narratio publish: executed=1 skipped=0 force=true; manifest=") {
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "") {
t.Fatalf("stdout = %q, want publish summary", stdout.String())
}
}
func TestExecutePublishRejectsUnsupportedArgsAndFlags(t *testing.T) {
cases := []struct {
name string
args []string
want string
}{
{name: "extra positional", args: []string{"publish", "2026-05-03", "extra"}, want: "publish: unexpected positional arguments"},
{name: "force flag", args: []string{"publish", "--force"}, want: "publish: invalid flags: flag provided but not defined: -force"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(tc.args, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), tc.want) {
t.Fatalf("stderr = %q, want %q", stderr.String(), tc.want)
}
})
}
}
func TestExecutePublishUnknownArtifactFailsValidation(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
@@ -362,23 +311,23 @@ func TestExecutePublishUnknownArtifactFailsValidation(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(
[]string{"publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "unknown_artifact"},
[]string{"run", "--stage", "publish", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "unknown_artifact"},
&stdout,
&stderr,
)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `publish: --artifacts includes unknown artifact "unknown_artifact"`) {
if !strings.Contains(stderr.String(), `--artifacts includes unknown artifact "unknown_artifact"`) {
t.Fatalf("stderr = %q, want unknown-artifact validation error", stderr.String())
}
}
func TestExecutePublishMissingConfigUsesRunStageLoadingPath(t *testing.T) {
func TestExecutePublishMissingConfigUsesSharedAcquisition(t *testing.T) {
deps, _, executor := guardedMissingPipelineDiscovery(t)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithDependencies([]string{"publish", "2026-05-03"}, &stdout, &stderr, deps, executor)
code := executeWithDependencies([]string{"run", "2026-05-03", "--stage", "publish", "--force"}, &stdout, &stderr, deps, executor)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
@@ -390,21 +339,6 @@ func TestExecutePublishMissingConfigUsesRunStageLoadingPath(t *testing.T) {
}
}
func TestExecuteUsageIncludesAnalyzeAndPublish(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(nil, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "analyze") {
t.Fatalf("stderr = %q, want usage to include analyze", stderr.String())
}
if !strings.Contains(stderr.String(), "publish") {
t.Fatalf("stderr = %q, want usage to include publish", stderr.String())
}
}
func writeValidConfigFilesWithScriptoriumArtifacts(t *testing.T, workspaceRoot string) (string, string, string) {
t.Helper()

View File

@@ -66,7 +66,7 @@ characters:
}
}
func TestArtifactSelectionFlagNormalize(t *testing.T) {
func TestNormalizeArtifactNames(t *testing.T) {
tests := []struct {
name string
inputs []string
@@ -92,14 +92,7 @@ func TestArtifactSelectionFlagNormalize(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var flag artifactSelectionFlag
for _, in := range tt.inputs {
if err := flag.Set(in); err != nil {
t.Fatalf("Set(%q) error = %v", in, err)
}
}
got, err := flag.Normalize()
got, err := normalizeArtifactNames(tt.inputs)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("Normalize() error = nil, want %q", tt.wantErr)
@@ -124,7 +117,7 @@ func TestArtifactSelectionFlagNormalize(t *testing.T) {
}
}
func TestValidateSelectedArtifacts(t *testing.T) {
func TestResolveSelectedArtifacts(t *testing.T) {
tests := []struct {
name string
cfg *config.Config
@@ -188,7 +181,10 @@ func TestValidateSelectedArtifacts(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateSelectedArtifacts(tt.cfg, tt.selected)
if tt.cfg.Pipeline != nil && tt.cfg.Pipeline.Scriptorium != nil {
tt.cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: tt.cfg.Pipeline.Scriptorium.Artifacts}
}
_, err := resolveEffectiveArtifacts(tt.cfg, tt.selected)
if tt.wantErr != "" {
if err == nil {
t.Fatalf("error = nil, want %q", tt.wantErr)

View File

@@ -0,0 +1,132 @@
package app
import (
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func buildHelperArtifactCatalog(cfg *config.Config, m *manifest.Manifest) (*artifacts.ArtifactCatalog, error) {
configured := artifacts.ConfiguredArtifactDefinitions(nil)
if cfg.Pipeline.Scriptorium != nil {
configured = artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())
}
extractionDefinitions := artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)
effective, err := artifacts.ResolveEffectiveArtifactSet(configured, nil)
if err != nil {
return nil, err
}
catalog, err := artifacts.BootstrapRuntimeCatalog(configured, effective, extractionDefinitions)
if err != nil {
return nil, err
}
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
if cfg.Pipeline.Notarius != nil && cfg.Pipeline.Notarius.Enabled {
catalog.HydrateExtractionArtifacts(paths, m, extractionDefinitions)
}
catalog.HydrateAnalyzeArtifacts(paths, m, configured)
return catalog, nil
}
func remoteLockSourceSet(locks []artifacts.RemoteLock) map[string]struct{} {
out := make(map[string]struct{}, len(locks))
for _, lock := range locks {
out[lock.Source] = struct{}{}
}
return out
}
func committedPublishOutputOutcomes(m *manifest.Manifest) map[string]string {
out := map[string]string{}
if m == nil || m.Stages["publish"] == nil {
return out
}
raw := m.Stages["publish"].Metadata["publish_output_outcomes"]
appendOutcome := func(item map[string]any) {
source, sourceOK := item["source"].(string)
status, statusOK := item["status"].(string)
if sourceOK && statusOK && isPublishOutputOutcome(status) {
out[source] = status
}
}
switch items := raw.(type) {
case []any:
for _, rawItem := range items {
if item, ok := rawItem.(map[string]any); ok {
appendOutcome(item)
}
}
case []map[string]any:
for _, item := range items {
appendOutcome(item)
}
}
return out
}
func isPublishOutputOutcome(status string) bool {
switch status {
case "published", "carried_locked", "locked_unpublished", "optional_missing", "unselected":
return true
default:
return false
}
}
func committedPublishedOutput(commit *artifacts.RemoteCommitManifest, source string) (artifacts.RemoteArtifact, bool) {
if commit == nil {
return artifacts.RemoteArtifact{}, false
}
for _, artifact := range commit.Artifacts {
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && artifact.Source == source {
return artifact, true
}
}
return artifacts.RemoteArtifact{}, false
}
func helperPublishedOutputDest(rule config.PublishOutputRule, catalog *artifacts.ArtifactCatalog) (string, bool, error) {
source := strings.TrimSpace(rule.Source)
normalized, err := artifactpolicy.ResolvePublishedDestinationWithExtractions(
source,
rule.Dest,
helperConfiguredOutputPathMap(catalog),
helperExtractionOutputSet(catalog),
)
if err != nil {
return "", false, err
}
entry, ok := catalog.Lookup(source)
showDest := !ok || strings.TrimSpace(entry.CanonicalRelPath) != normalized
return normalized, showDest, nil
}
func helperExtractionOutputSet(catalog *artifacts.ArtifactCatalog) map[string]struct{} {
out := map[string]struct{}{}
if catalog == nil {
return out
}
for _, entry := range catalog.ListExtraction() {
if strings.TrimSpace(entry.ExtractionKey) != "" {
out[entry.ExtractionKey] = struct{}{}
}
}
return out
}
func helperConfiguredOutputPathMap(catalog *artifacts.ArtifactCatalog) map[string]string {
out := map[string]string{}
if catalog == nil {
return out
}
for _, entry := range catalog.ListConfigured() {
if strings.TrimSpace(entry.ConfiguredKey) == "" {
continue
}
out[entry.ConfiguredKey] = strings.TrimSpace(entry.CanonicalRelPath)
}
return out
}

View File

@@ -22,6 +22,7 @@ func TestBuildHelperArtifactCatalogUsesAnalyzeManifestEvidence(t *testing.T) {
},
Session: &config.SessionConfig{Campaign: "campaign", SessionID: "session"},
}
cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: cfg.Pipeline.Scriptorium.Artifacts}
paths := artifacts.NewLocalStore(root).SessionPathsFor("campaign", "session")
outputPath := filepath.Join(paths.ArtifactsDir, "session_recap.md")
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {

View File

@@ -38,7 +38,7 @@ func TestAssembledFullRunUsesCanonicalOrder(t *testing.T) {
}
}
func TestAssembledCanonicalAndAliasArtifactRegenerationRequestsMatch(t *testing.T) {
func TestAssembledForcedRangeAndRegenerationRequestsMatch(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
type capturedRequest struct {
@@ -55,23 +55,22 @@ func TestAssembledCanonicalAndAliasArtifactRegenerationRequestsMatch(t *testing.
}
base := []string{
"2026-05-03", "--force", "--from", "extract", "--through", "analyze",
"--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath,
"--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath,
}
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
if err := runWithDependencies(context.Background(), base, &bytes.Buffer{}, executor, deps); err != nil {
if err := invokeNativeCommandWith(context.Background(), append([]string{"run"}, base...), &bytes.Buffer{}, executor, deps); err != nil {
t.Fatalf("canonical unselected Run() error = %v", err)
}
selected := append(append([]string(nil), base...), "--artifacts", "session_recap")
if err := runWithDependencies(context.Background(), selected, &bytes.Buffer{}, executor, deps); err != nil {
if err := invokeNativeCommandWith(context.Background(), append([]string{"run"}, selected...), &bytes.Buffer{}, executor, deps); err != nil {
t.Fatalf("canonical selected Run() error = %v", err)
}
var stdout, stderr bytes.Buffer
alias := []string{
"regenerate-artifacts", "2026-05-03", "--artifacts", "session_recap",
"--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath,
regeneration := []string{"artifacts", "regenerate", "2026-05-03", "--artifacts", "session_recap",
"--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath,
}
if code := executeWithDependencies(alias, &stdout, &stderr, deps, executor); code != 0 {
t.Fatalf("alias exit=%d stderr=%q", code, stderr.String())
if code := executeWithDependencies(regeneration, &stdout, &stderr, deps, executor); code != 0 {
t.Fatalf("regeneration exit=%d stderr=%q", code, stderr.String())
}
if len(captured) != 3 {
t.Fatalf("captured requests = %#v", captured)
@@ -82,7 +81,7 @@ func TestAssembledCanonicalAndAliasArtifactRegenerationRequestsMatch(t *testing.
}
if !reflect.DeepEqual(captured[1], captured[2]) || !reflect.DeepEqual(captured[1].stages, wantStages) ||
!reflect.DeepEqual(captured[1].artifacts, []string{"session_recap"}) || !captured[1].force {
t.Fatalf("canonical=%#v alias=%#v, want identical bounded request", captured[1], captured[2])
t.Fatalf("forced range=%#v regeneration=%#v, want identical bounded request", captured[1], captured[2])
}
}
@@ -151,6 +150,7 @@ func TestAssembledLegacyAnalyzeTransitionPublishesOnlyCurrentRecords(t *testing.
{Source: artifacts.ConfiguredArtifactSourceID("session_recap"), Dest: "artifacts/session_recap.md", Required: boolPtr(true)},
},
}
cfg.ArtifactCatalog.PublishOutputs = cfg.Pipeline.Publish.Outputs
paths, err := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).EnsureLayoutFor(cfg.Session.Campaign, cfg.Session.SessionID)
if err != nil {
t.Fatal(err)
@@ -261,15 +261,14 @@ func TestAssembledSplitBundleCommandsUseOneEffectiveFamilyConfiguration(t *testi
}
common := []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath,
"--session", sessionPath, "--overlay", "testing",
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--overlay", "testing",
}
var planned bytes.Buffer
if err := Plan(context.Background(), common, &planned); err != nil {
if err := invokeNativeCommand(context.Background(), append([]string{"session", "plan"}, common...), &planned); err != nil {
t.Fatalf("plan testing bundle: %v", err)
}
if !strings.Contains(planned.String(), "overlay=testing") {
t.Fatalf("plan output = %q, want testing provenance", planned.String())
if report := decodeNativeReport[sessionPlanReport](t, planned.Bytes()); report.Campaign != "sample-campaign" || len(report.Stages) != len(canonicalStageNames()) {
t.Fatalf("plan=%#v", report)
}
var capturedCfg *config.Config
@@ -283,7 +282,7 @@ func TestAssembledSplitBundleCommandsUseOneEffectiveFamilyConfiguration(t *testi
}
runArgs := append(append([]string(nil), common...), "--from", "analyze", "--through", "analyze", "--artifacts", "character_items_arannis")
var runOut bytes.Buffer
if err := runWithPipelineExecutor(context.Background(), runArgs, &runOut, executor); err != nil {
if err := invokeNativeCommandWith(context.Background(), append([]string{"run"}, runArgs...), &runOut, executor, commandDependencies{}); err != nil {
t.Fatalf("bounded family run: %v", err)
}
if !reflect.DeepEqual(capturedPlan.Names(), []string{"analyze"}) {
@@ -292,7 +291,7 @@ func TestAssembledSplitBundleCommandsUseOneEffectiveFamilyConfiguration(t *testi
if profile, ok := config.SelectedPipelineOverlay(capturedCfg.Pipeline); !ok || profile.Name != "testing" {
t.Fatalf("captured profile = %#v, selected=%t", profile, ok)
}
if digest := config.EffectivePipelineDigest(capturedCfg.Pipeline); digest == "" || !strings.Contains(runOut.String(), "digest="+digest) {
if digest := config.EffectivePipelineDigest(capturedCfg.Pipeline); digest == "" {
t.Fatalf("run output = %q, want effective digest %q", runOut.String(), digest)
}
if got := capturedOptions.EffectiveArtifacts.Keys(); !reflect.DeepEqual(got, []string{"character_items_arannis"}) {

View File

@@ -1,118 +0,0 @@
package app
import (
"flag"
"fmt"
"io"
"strconv"
)
type boundedRunRequest struct {
Config commonConfigFlags
Plan BoundedPlan
Force bool
SelectedArtifacts []string
}
type singletonStringFlag struct {
name string
value string
set bool
}
func (f *singletonStringFlag) String() string { return f.value }
func (f *singletonStringFlag) Set(value string) error {
if f.set {
return fmt.Errorf("--%s may be specified only once", f.name)
}
f.value = value
f.set = true
return nil
}
func (f singletonStringFlag) pointer() *string {
if !f.set {
return nil
}
value := f.value
return &value
}
type singletonBoolFlag struct {
name string
value bool
set bool
}
func (f *singletonBoolFlag) String() string { return strconv.FormatBool(f.value) }
func (f *singletonBoolFlag) IsBoolFlag() bool { return true }
func (f *singletonBoolFlag) Set(raw string) error {
if f.set {
return fmt.Errorf("--%s may be specified only once", f.name)
}
value, err := strconv.ParseBool(raw)
if err != nil {
return fmt.Errorf("--%s requires a boolean value: %w", f.name, err)
}
f.value = value
f.set = true
return nil
}
func parseBoundedRunRequest(command string, args []string, help io.Writer) (boundedRunRequest, error) {
fs := flag.NewFlagSet(command, flag.ContinueOnError)
fs.SetOutput(help)
var configFlags commonConfigFlags
var from singletonStringFlag
var through singletonStringFlag
var force singletonBoolFlag
var selectedArtifacts artifactSelectionFlag
from.name = "from"
through.name = "through"
force.name = "force"
addCommonConfigFlags(fs, &configFlags)
fs.Var(&from, "from", "first canonical stage to select (inclusive)")
fs.Var(&through, "through", "last canonical stage to select (inclusive)")
fs.Var(&force, "force", "rerun selected stages even when already succeeded")
fs.Var(&selectedArtifacts, "artifacts", "configured artifact names to execute and publish (comma-separated or repeatable)")
fs.Usage = func() {
invocation := "narratio run"
if command == "plan" {
invocation = "narratio session plan"
}
_, _ = fmt.Fprintf(help, "Usage: %s <session_id> [--from <stage>] [--through <stage>] [--force] [--artifacts <name[,name...]>] [common config flags]\n\n", invocation)
_, _ = fmt.Fprintln(help, "Bounds are inclusive; omitted --from or --through selects the beginning or end of the canonical pipeline.")
_, _ = fmt.Fprintln(help)
_, _ = fmt.Fprintln(help, "Flags:")
fs.PrintDefaults()
}
if err := parseSessionAwareFlags(command, fs, args, &configFlags.sessionID); err != nil {
return boundedRunRequest{}, err
}
if configFlags.sessionID == "" {
return boundedRunRequest{}, fmt.Errorf("%s: session_id is required", command)
}
plan, err := BuildBoundedPlan(from.value, through.value)
if err != nil {
return boundedRunRequest{}, fmt.Errorf("%s: %w", command, err)
}
normalizedArtifacts, err := selectedArtifacts.Normalize()
if err != nil {
return boundedRunRequest{}, fmt.Errorf("%s: invalid --artifacts: %w", command, err)
}
if len(normalizedArtifacts) > 0 && !plan.Contains("analyze") && !plan.Contains("publish") {
return boundedRunRequest{}, fmt.Errorf("%s: --artifacts requires a selected range containing analyze or publish", command)
}
return boundedRunRequest{
Config: configFlags,
Plan: plan,
Force: force.value,
SelectedArtifacts: normalizedArtifacts,
}, nil
}

View File

@@ -3,10 +3,10 @@ package app
import (
"bytes"
"context"
"io"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@@ -14,125 +14,6 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func TestBoundedRunParsingIsSharedByRunAndPlan(t *testing.T) {
args := []string{
"2026-05-03",
"--from", "extract",
"--through=publish",
"--force",
"--artifacts", "session_recap,player_handout",
"--artifacts=session_recap",
"--config", "pipeline.yml",
}
runRequest, err := parseBoundedRunRequest("run", args, io.Discard)
if err != nil {
t.Fatalf("parse run request: %v", err)
}
planRequest, err := parseBoundedRunRequest("plan", args, io.Discard)
if err != nil {
t.Fatalf("parse plan request: %v", err)
}
if !reflect.DeepEqual(runRequest.Plan.Names(), planRequest.Plan.Names()) ||
runRequest.Plan.From() != planRequest.Plan.From() ||
runRequest.Plan.Through() != planRequest.Plan.Through() ||
runRequest.Force != planRequest.Force ||
!reflect.DeepEqual(runRequest.SelectedArtifacts, planRequest.SelectedArtifacts) ||
runRequest.Config != planRequest.Config {
t.Fatalf("run request = %#v, plan request = %#v", runRequest, planRequest)
}
wantArtifacts := []string{"player_handout", "session_recap"}
if !reflect.DeepEqual(runRequest.SelectedArtifacts, wantArtifacts) {
t.Fatalf("artifacts = %#v, want %#v", runRequest.SelectedArtifacts, wantArtifacts)
}
}
func TestBoundedRunParsingRejectsDuplicateSingletons(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{name: "from separate", args: []string{"session", "--from", "render", "--from", "extract"}, want: "--from may be specified only once"},
{name: "from equals", args: []string{"session", "--from=render", "--from=extract"}, want: "--from may be specified only once"},
{name: "through mixed", args: []string{"session", "--through", "analyze", "--through=publish"}, want: "--through may be specified only once"},
{name: "force separate", args: []string{"session", "--force", "--force"}, want: "--force may be specified only once"},
{name: "force equals", args: []string{"session", "--force=true", "--force=false"}, want: "--force may be specified only once"},
{name: "overlay", args: []string{"session", "--overlay", "production", "--overlay=testing"}, want: "--overlay may be specified only once"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := parseBoundedRunRequest("run", test.args, io.Discard)
if err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("error = %v, want %q", err, test.want)
}
})
}
}
func TestBoundedRunParsingRetainsExplicitOverlayPresence(t *testing.T) {
withoutOverlay, err := parseBoundedRunRequest("run", []string{"session"}, io.Discard)
if err != nil {
t.Fatal(err)
}
if got := withoutOverlay.Config.sessionOptions().Overlay; got != nil {
t.Fatalf("omitted overlay = %#v, want nil", got)
}
withOverlay, err := parseBoundedRunRequest("run", []string{"session", "--overlay", "testing"}, io.Discard)
if err != nil {
t.Fatal(err)
}
if got := withOverlay.Config.sessionOptions().Overlay; got == nil || *got != "testing" {
t.Fatalf("explicit overlay = %#v, want testing", got)
}
explicitEmpty, err := parseBoundedRunRequest("run", []string{"session", "--overlay", ""}, io.Discard)
if err != nil {
t.Fatal(err)
}
if got := explicitEmpty.Config.sessionOptions().Overlay; got == nil || *got != "" {
t.Fatalf("explicit empty overlay = %#v, want non-nil empty", got)
}
}
func TestBoundedRunParsingUsesSharedRangeValidation(t *testing.T) {
args := []string{"session", "--from", "publish", "--through", "render"}
runRequest, runErr := parseBoundedRunRequest("run", args, io.Discard)
planRequest, planErr := parseBoundedRunRequest("plan", args, io.Discard)
if runErr == nil || planErr == nil {
t.Fatalf("run request=%#v error=%v; plan request=%#v error=%v", runRequest, runErr, planRequest, planErr)
}
runDetail := strings.TrimPrefix(runErr.Error(), "run: ")
planDetail := strings.TrimPrefix(planErr.Error(), "plan: ")
if runDetail != planDetail || !strings.Contains(runDetail, `from stage "publish" occurs after through stage "render"`) {
t.Fatalf("run error = %q, plan error = %q", runErr, planErr)
}
}
func TestBoundedRunParsingGatesArtifactSelectionByRange(t *testing.T) {
for _, test := range []struct {
name string
through string
wantErr bool
}{
{name: "render only", through: "render", wantErr: true},
{name: "analyze only", through: "analyze"},
{name: "publish only", through: "publish"},
} {
t.Run(test.name, func(t *testing.T) {
_, err := parseBoundedRunRequest("run", []string{
"session", "--from", test.through, "--through", test.through, "--artifacts", "session_recap",
}, io.Discard)
if test.wantErr && (err == nil || !strings.Contains(err.Error(), "range containing analyze or publish")) {
t.Fatalf("error = %v, want artifact/range error", err)
}
if !test.wantErr && err != nil {
t.Fatalf("error = %v", err)
}
})
}
}
func TestRunPassesBoundedPlanToRunner(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
@@ -148,15 +29,13 @@ func TestRunPassesBoundedPlanToRunner(t *testing.T) {
}
var out bytes.Buffer
err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03",
err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03",
"--from", "render",
"--through", "extract",
"--force",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
}, &out, executor)
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &out, executor, commandDependencies{})
if err != nil {
t.Fatalf("Run() error = %v", err)
}
@@ -179,44 +58,18 @@ func TestPlanPrintsOnlyBoundedRange(t *testing.T) {
t.Fatalf("save prerequisite manifest: %v", err)
}
var out bytes.Buffer
err := Plan(context.Background(), []string{
"2026-05-03",
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03",
"--from", "render",
"--through", "extract",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &out)
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
got := out.String()
if !strings.Contains(got, "render: run\nextract: run\ntotals: run=2 skip=0") {
t.Fatalf("output = %q, want bounded decisions", got)
report := decodeNativeReport[sessionPlanReport](t, out.Bytes())
if len(report.Stages) != 2 || report.Stages[0].Name != "render" || report.Stages[1].Name != "extract" || report.Stages[0].Action != "run" || report.Stages[1].Action != "run" {
t.Fatalf("bounded plan=%#v", report)
}
if strings.Contains(got, "trim: ") || strings.Contains(got, "analyze: ") {
t.Fatalf("output = %q, contains excluded stages", got)
}
}
func TestBoundedRunCommandHelp(t *testing.T) {
for _, test := range []struct {
name string
args []string
want string
}{
{name: "run", args: []string{"run", "--help"}, want: "Usage: narratio run <session_id> [--from <stage>] [--through <stage>]"},
{name: "plan", args: []string{"session", "plan", "--help"}, want: "Usage: narratio session plan <session_id> [--from <stage>] [--through <stage>]"},
} {
t.Run(test.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
if code := executeForTest(test.args, &stdout, &stderr); code != 0 {
t.Fatalf("exit code = %d, stderr = %q", code, stderr.String())
}
if !strings.Contains(stdout.String(), test.want) || stderr.Len() != 0 {
t.Fatalf("stdout = %q stderr = %q, want %q", stdout.String(), stderr.String(), test.want)
}
})
}
}

View File

@@ -207,7 +207,7 @@ func cleanSessionCache(ctx context.Context, cfg *config.Config, dryRun bool, dep
if cfg.Pipeline.Storage.S3 == nil || strings.TrimSpace(cfg.Pipeline.Storage.S3.Bucket) == "" {
return nil, fmt.Errorf("clear cache requires pipeline.storage.s3.bucket")
}
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, deps)
if err != nil {
return nil, fmt.Errorf("initialize object store for cache cleanup: %w", err)
}

View File

@@ -164,7 +164,7 @@ func TestCleanupFailurePreservesPublishAndRetriesWithoutRepublishing(t *testing.
}
}
func TestRunStageCleanupUsesPublishEvidenceAndFailsClosedWithoutIt(t *testing.T) {
func TestRunSingleStageCleanupUsesPublishEvidenceAndFailsClosedWithoutIt(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
enableCleanupForCommandTest(t, pipelinePath)
@@ -192,14 +192,15 @@ func TestRunStageCleanupUsesPublishEvidenceAndFailsClosedWithoutIt(t *testing.T)
}
}
seedInvocationPrerequisites(t, workspaceRoot, "cleanup")
var output bytes.Buffer
if err := RunStage(context.Background(), []string{
"cleanup", cfg.Session.SessionID, "--config", pipelinePath,
"--campaign-file", campaignPath, "--session", sessionPath,
if err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "cleanup", cfg.Session.SessionID, "--config", pipelinePath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &output); err != nil {
t.Fatalf("RunStage(cleanup): %v", err)
t.Fatalf("single-stage run (cleanup): %v", err)
}
if !strings.Contains(output.String(), "stage=cleanup executed=1 skipped=0") {
if !runCountsMatch(t, output.Bytes(), 1, 0, "cleanup") {
t.Fatalf("output = %q, want direct cleanup execution", output.String())
}
for _, target := range []string{workspaceTarget, spoolTarget} {
@@ -218,12 +219,12 @@ func TestRunStageCleanupUsesPublishEvidenceAndFailsClosedWithoutIt(t *testing.T)
missingRoot := t.TempDir()
missingPipeline, missingCampaign, missingSession := writeValidConfigFiles(t, missingRoot)
enableCleanupForCommandTest(t, missingPipeline)
err = RunStage(context.Background(), []string{
"cleanup", "2026-05-03", "--config", missingPipeline,
"--campaign-file", missingCampaign, "--session", missingSession,
seedInvocationPrerequisites(t, missingRoot, "cleanup")
err = invokeNativeCommand(context.Background(), []string{"run", "--stage", "cleanup", "2026-05-03", "--config", missingPipeline,
"--campaign-file", missingCampaign, "--session-file", missingSession,
}, &bytes.Buffer{})
if err == nil || !strings.Contains(err.Error(), "cleanup requires a recorded publish stage") {
t.Fatalf("RunStage(cleanup) without evidence error = %v", err)
if err == nil || !strings.Contains(err.Error(), "publish") {
t.Fatalf("single-stage run (cleanup) without evidence error = %v", err)
}
}

View File

@@ -44,5 +44,13 @@ func consoleFields(fields []any) commandkit.Detail {
func withCommandReporting(deps commandDependencies, invocation commandkit.Invocation) commandDependencies {
deps.reporter = nativeConsole{reporter: invocation.Reporter()}
deps.diagnostics = bootstrapInfoWriter{reporter: invocation.Reporter()}
return deps
}
type bootstrapInfoWriter struct{ reporter *commandkit.Reporter }
func (w bootstrapInfoWriter) Write(body []byte) (int, error) {
_ = w.reporter.Info("", func(destination io.Writer) error { _, err := destination.Write(body); return err })
return len(body), nil
}

View File

@@ -22,21 +22,21 @@ type configurationFlags[I any] struct {
func newConfigurationFlags[I any](selection func(*I) *configurationSelection) configurationFlags[I] {
return configurationFlags[I]{
Config: commandkit.ValueFlag(commandkit.FlagMeta{Name: "config", Usage: "Pipeline configuration file."}, nil, stringValue,
Config: commandkit.ValueFlag(commandkit.FlagMeta{Name: "config", Completion: commandkit.CompleteFiles(), Usage: "Pipeline configuration file."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).Config = v }),
Overlay: commandkit.ValueFlag(commandkit.FlagMeta{Name: "overlay", Usage: "Pipeline overlay."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).Overlay = v }),
Campaign: commandkit.ValueFlag(commandkit.FlagMeta{Name: "campaign", Usage: "Assert the campaign identity."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).Campaign = v }),
CampaignFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "campaign-file", Usage: "Campaign configuration file."}, nil, stringValue,
CampaignFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "campaign-file", Completion: commandkit.CompleteFiles(), Usage: "Campaign configuration file."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).CampaignFile = v }),
Session: commandkit.ValueFlag(commandkit.FlagMeta{Name: "session", Usage: "Assert the session identity for configuration inspection."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).Session = v }),
SessionFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "session-file", Usage: "Session configuration file."}, nil, stringValue,
SessionFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "session-file", Completion: commandkit.CompleteFiles(), Usage: "Session configuration file."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).SessionFile = v }),
PartyFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "party-file", Usage: "Party file replacing the campaign default."}, nil, stringValue,
PartyFile: commandkit.ValueFlag(commandkit.FlagMeta{Name: "party-file", Completion: commandkit.CompleteFiles(), Usage: "Party file replacing the campaign default."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).PartyFile = v }),
SecretsDir: commandkit.ValueFlag(commandkit.FlagMeta{Name: "secrets-dir", Usage: "Credential-bootstrap directory."}, nil, stringValue,
SecretsDir: commandkit.ValueFlag(commandkit.FlagMeta{Name: "secrets-dir", Completion: commandkit.CompleteDirectories(), Usage: "Credential-bootstrap directory."}, nil, stringValue,
func(in *I, v commandkit.Supplied[string]) { selection(in).SecretsDir = v }),
}
}

View File

@@ -4,11 +4,14 @@ import (
"context"
"io"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
)
func executeForTest(args []string, stdout, stderr io.Writer) int {
return Execute(context.Background(), args, commandkit.Streams{Out: stdout, Err: stderr}).Status
return executeWithDependencies(args, stdout, stderr, commandDependencies{}, executePlan)
}
func executeWithPipelineExecutor(args []string, stdout, stderr io.Writer, executor pipelineExecutor) int {
@@ -16,6 +19,9 @@ func executeWithPipelineExecutor(args []string, stdout, stderr io.Writer, execut
}
func executeWithDependencies(args []string, stdout, stderr io.Writer, deps commandDependencies, executor pipelineExecutor) int {
if deps.newObjectStore == nil {
deps.newObjectStore = func(context.Context, *config.Config) (storage.ObjectStore, error) { return &storage.FakeBackend{}, nil }
}
application, err := newApplication(deps, executor)
if err != nil {
panic(err)

View File

@@ -16,6 +16,7 @@ func NewApplication() (*commandkit.App, error) {
func newApplication(deps commandDependencies, executor pipelineExecutor) (*commandkit.App, error) {
return commandkit.New(
commandkit.Identity{Name: "narratio", Version: buildinfo.Version},
commandkit.Completion("narratio"),
configurationCommand(deps),
executionCommand("run", "Execute a session pipeline.", false, deps, executor),
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts.",

View File

@@ -11,6 +11,7 @@ import (
"strings"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
@@ -27,6 +28,10 @@ func TestNativeDiscoveryDoesNotAcquireConfiguration(t *testing.T) {
t.Fatal("discovery loaded pipeline configuration")
return nil, nil
},
newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
t.Fatal("discovery initialized object storage")
return nil, nil
},
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
t.Fatal("discovery bootstrapped credentials")
return nil, nil
@@ -35,13 +40,35 @@ func TestNativeDiscoveryDoesNotAcquireConfiguration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, args := range [][]string{nil, {"help"}, {"--help"}, {"session"}, {"artifacts"}, {"version"}} {
for _, args := range [][]string{nil, {"help"}, {"--help"}, {"session"}, {"artifacts"}, {"version"}, {"help", "run"}, {"help", "config", "show"}, {"session", "locks"}, {"config", "scopes"}, {"completion"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}} {
var out, diagnostics bytes.Buffer
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics})
if outcome.Status != 0 || out.Len() == 0 || diagnostics.Len() != 0 {
t.Fatalf("%v: outcome=%+v stdout=%q stderr=%q", args, outcome, out.String(), diagnostics.String())
}
}
for _, query := range []struct {
args []string
fragment string
}{
{[]string{"__complete", "run", "--config", "missing.yml", "--secrets-dir", "missing-secrets", "--stage", "tr"}, "transcribe\n"},
{[]string{"__complete", "session", "plan", "--from", "an"}, "analyze\n"},
{[]string{"__complete", "run", "--through", "pub"}, "publish\n"},
{[]string{"__complete", "config", "show", "--scope", ""}, "party\n"},
{[]string{"__complete", "run", "--config", ""}, ":0\n"},
{[]string{"__complete", "session", "status", "--session-file", ""}, ":0\n"},
{[]string{"__complete", "session", "plan", "--party-file", ""}, ":0\n"},
{[]string{"__complete", "config", "diff", "--scope", "party", "--against-party-file", ""}, ":0\n"},
{[]string{"__complete", "session", "init", "--output", ""}, ":0\n"},
{[]string{"__complete", "session", "init", "--audio-dir", ""}, ":16\n"},
{[]string{"__complete", "run", "--secrets-dir", ""}, ":16\n"},
} {
var out, diagnostics bytes.Buffer
outcome := application.Execute(context.Background(), query.args, commandkit.Streams{Out: &out, Err: &diagnostics})
if outcome.Status != 0 || diagnostics.Len() != 0 || !strings.Contains(out.String(), query.fragment) {
t.Fatalf("query=%v outcome=%#v reply=%q diagnostic=%s", query.args, outcome, out.String(), diagnostics.String())
}
}
if outcome := application.Execute(context.Background(), []string{"unknown"}, commandkit.Streams{}); outcome.Status != 2 {
t.Fatalf("invalid invocation: %+v", outcome)
}
@@ -114,3 +141,27 @@ func TestOperationOutcomePreservesCausesAndOperationalPrecedence(t *testing.T) {
t.Fatalf("failure delivery: outcome=%+v diagnostics=%q", outcome, diagnostics.String())
}
}
func TestNativeTreeRejectsRemovedRoutesAndMissingOperandsBeforeAcquisition(t *testing.T) {
application, err := newApplication(commandDependencies{
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
t.Fatal("invalid input loaded configuration")
return nil, nil
},
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
t.Fatal("invalid input bootstrapped credentials")
return nil, nil
},
}, nil)
if err != nil {
t.Fatal(err)
}
cases := [][]string{{"run"}, {"artifacts", "regenerate"}, {"session", "init"}, {"session", "validate"}, {"session", "status"}, {"session", "plan"}, {"session", "artifacts"}, {"session", "restore"}, {"session", "clean"}, {"session", "locks", "list"}, {"session", "locks", "add"}, {"session", "locks", "remove"}, {"run-stage"}, {"analyze"}, {"publish"}, {"regenerate-artifacts"}, {"clean"}, {"plan"}, {"status"}, {"resume"}, {"lock"}, {"unlock"}, {"config", "artifacts"}, {"run", "session", "--session-id", "session"}, {"session", "status", "session", "--manifest", "manifest.json"}}
for _, args := range cases {
var out, diagnostic bytes.Buffer
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
if outcome.Status != 2 {
t.Fatalf("args=%v status=%d diagnostic=%s", args, outcome.Status, diagnostic.String())
}
}
}

View File

@@ -3,6 +3,7 @@ package app
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
@@ -26,110 +27,24 @@ func TestExecuteValidCommands(t *testing.T) {
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot, srv.URL)
cases := []struct {
name string
args []string
wantOut string
}{
{name: "run", args: []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, wantOut: "narratio run: session 2026-05-03; executed=12 skipped=2; manifest="},
{name: "session plan", args: []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, wantOut: "analyze: skip\n targets: none\n prerequisites: none\n execute: none\n reuse: none\npublish: skip\nnotify: skip"},
{name: "session status", args: []string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, wantOut: "Session: 2026-05-03"},
{name: "run-stage", args: []string{"run-stage", "polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, wantOut: "narratio run-stage: stage=polish executed=0 skipped=1 force=false; manifest="},
for _, command := range [][]string{{"run", "2026-05-03"}, {"session", "plan", "2026-05-03"}, {"session", "status", "2026-05-03"}, {"run", "2026-05-03", "--stage", "polish"}} {
args := append(command, "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--format", "json")
var stdout, stderr bytes.Buffer
if code := executeForTest(args, &stdout, &stderr); code != 0 || stderr.Len() != 0 {
t.Fatalf("args=%v status=%d diagnostics=%s", args, code, stderr.String())
}
var report map[string]any
if err := json.Unmarshal(stdout.Bytes(), &report); err != nil {
t.Fatal(err)
}
if report["session_id"] != "2026-05-03" {
t.Fatalf("report=%v", report)
}
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(tc.args, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0", code)
}
if stderr.Len() != 0 {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
if !strings.Contains(stdout.String(), tc.wantOut) {
t.Fatalf("stdout = %q, want to contain %q", stdout.String(), tc.wantOut)
}
})
}
}
func TestExecuteMissingRequiredFlags(t *testing.T) {
cases := []struct {
name string
args []string
want string
controlledDiscovery bool
}{
{name: "run missing session", args: []string{"run"}, want: "run: session_id is required"},
{name: "plan old top-level removed", args: []string{"plan"}, want: `unknown command: "plan"`},
{name: "status old top-level removed", args: []string{"status"}, want: `unknown command: "status"`},
{name: "resume removed", args: []string{"resume"}, want: `unknown command: "resume"`},
{name: "run-stage missing name", args: []string{"run-stage", "--config", "a", "--session", "b"}, want: "run-stage: expected stage name and session_id"},
{name: "run-stage missing session", args: []string{"run-stage", "polish"}, want: "run-stage: expected stage name and session_id"},
{name: "run missing config uses defaults", args: []string{"run", "2026-05-03", "--session", "session.yml"}, want: "source.missing", controlledDiscovery: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
deps := commandDependencies{}
executor := pipelineExecutor(executePlan)
if tc.controlledDiscovery {
deps, _, executor = guardedMissingPipelineDiscovery(t)
}
code := executeWithDependencies(tc.args, &stdout, &stderr, deps, executor)
if code == 0 {
t.Fatalf("exit code = 0, want non-zero")
}
if stdout.Len() != 0 {
t.Fatalf("stdout = %q, want empty", stdout.String())
}
if !strings.Contains(stderr.String(), tc.want) {
t.Fatalf("stderr = %q, want to contain %q", stderr.String(), tc.want)
}
})
}
}
func TestExecuteRunStageUnknownFails(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot, "https://example.com/transcribe")
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run-stage", "unknown", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "unknown stage") {
t.Fatalf("stderr = %q, want unknown stage error", stderr.String())
}
}
func TestExecuteRunStageArchiveAliasFails(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot, "https://example.com/transcribe")
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run-stage", "archive", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `unknown stage "archive"`) {
t.Fatalf("stderr = %q, want unknown stage alias error", stderr.String())
}
}
func TestExecuteRunStageNormalizeIsAccepted(t *testing.T) {
func TestExecuteRunSingleStageNormalizeIsAccepted(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot, "https://example.com/transcribe")
workRoot := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03")
@@ -137,25 +52,27 @@ func TestExecuteRunStageNormalizeIsAccepted(t *testing.T) {
mustWriteTestFile(t, polishedPath, `{"segments":[{"id":1}]}`)
seedCurrentBuiltInEvidence(t, filepath.Join(workRoot, "manifest.json"), "2026-05-03", "sample-campaign", "polish", artifacts.TranscriptOutputKindPolished, artifacts.ArtifactTranscriptPolished, polishedPath)
seedInvocationPrerequisites(t, workspaceRoot, "normalize")
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run-stage", "normalize", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"run", "2026-05-03", "--stage", "normalize", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "stage=normalize executed=1 skipped=0") {
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "normalize") {
t.Fatalf("stdout = %q, want normalize stage execution", stdout.String())
}
}
func TestExecuteRunStageTranscribeUsesConfiguredWhisperXServer(t *testing.T) {
func TestExecuteRunSingleStageTranscribeUsesConfiguredWhisperXServer(t *testing.T) {
workspaceRoot := t.TempDir()
var serverCalls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serverCalls++
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"source":"run-stage-test","segments":[{"speaker":"alice"}]}`))
_, _ = w.Write([]byte(`{"source":"selected-stage-test","segments":[{"speaker":"alice"}]}`))
}))
defer srv.Close()
@@ -164,14 +81,14 @@ func TestExecuteRunStageTranscribeUsesConfiguredWhisperXServer(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run-stage", "prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"run", "2026-05-03", "--stage", "prepare", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr)
if code != 0 {
t.Fatalf("prepare exit code = %d, want 0; stderr=%q", code, stderr.String())
}
stdout.Reset()
stderr.Reset()
code = executeForTest([]string{"run-stage", "transcribe", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"}, &stdout, &stderr)
code = executeForTest([]string{"run", "2026-05-03", "--stage", "transcribe", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force"}, &stdout, &stderr)
if code != 0 {
t.Fatalf("transcribe exit code = %d, want 0; stderr=%q", code, stderr.String())
}
@@ -188,12 +105,12 @@ func TestExecuteRunStageTranscribeUsesConfiguredWhisperXServer(t *testing.T) {
if got == `{"schema":"speaker_transcript.v1","segments":[]}` {
t.Fatalf("got noop transcript output: %q", got)
}
if !strings.Contains(got, `"source":"run-stage-test"`) {
t.Fatalf("output = %q, want run-stage server json", got)
if !strings.Contains(got, `"source":"selected-stage-test"`) {
t.Fatalf("output = %q, want selected-stage server JSON", got)
}
}
func TestExecuteRunStagePolishLoadsCredentialFromSecretsDir(t *testing.T) {
func TestExecuteRunSingleStagePolishLoadsCredentialFromSecretsDir(t *testing.T) {
workspaceRoot := t.TempDir()
configDir := t.TempDir()
sessionID := "2026-05-03"
@@ -259,13 +176,15 @@ inputs:
seedCurrentBuiltInEvidence(t, filepath.Join(workRoot, "manifest.json"), sessionID, "sample-campaign", "merge", artifacts.TranscriptOutputKindBase, artifacts.ArtifactTranscriptBase, basePath)
mustWriteTestFile(t, filepath.Join(workRoot, "inputs", "glossary.yml"), "[]\n")
seedInvocationPrerequisites(t, workspaceRoot, "polish")
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run-stage", "polish", sessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force", "--secrets-dir", secretsDir}, &stdout, &stderr)
code := executeForTest([]string{"run", sessionID, "--stage", "polish", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force", "--secrets-dir", secretsDir}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "stage=polish executed=1 skipped=0") {
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "polish") {
t.Fatalf("stdout = %q, want polish execution", stdout.String())
}
recorded, err := (&manifest.LocalStore{}).Load(context.Background(), filepath.Join(workRoot, "manifest.json"))
@@ -351,7 +270,7 @@ inputs:
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--secrets-dir", filepath.Join(configDir, "missing-secrets")}, &stdout, &stderr)
code := executeForTest([]string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--secrets-dir", filepath.Join(configDir, "missing-secrets")}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
@@ -374,11 +293,11 @@ func TestExecuteUsesDefaultPipelineConfigPathWhenConfigFlagOmitted(t *testing.T)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run", "2026-05-03", "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"run", "2026-05-03", "--session-file", sessionPath}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "narratio run: session 2026-05-03; executed=12 skipped=2; manifest=") {
if !runCountsMatch(t, stdout.Bytes(), 12, 2, "") {
t.Fatalf("stdout = %q, want successful run output", stdout.String())
}
}
@@ -392,7 +311,7 @@ func TestExecuteMissingCampaignConfigReportsRegistryPath(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"run", "2026-05-03", "--config", pipelinePath, "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"run", "2026-05-03", "--config", pipelinePath, "--session-file", sessionPath}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
@@ -413,11 +332,11 @@ func TestExecuteUsesPipelineDefaultCampaignID(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"session", "status", "2026-05-03", "--config", pipelinePath, "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"session", "status", "2026-05-03", "--config", pipelinePath, "--session-file", sessionPath}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "Campaign: sample-campaign") {
if decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Campaign != "sample-campaign" {
t.Fatalf("stdout = %q, want default campaign", stdout.String())
}
}
@@ -442,30 +361,15 @@ inputs:
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign", "icewind", "--session", sessionPath}, &stdout, &stderr)
code := executeForTest([]string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign", "icewind", "--session-file", sessionPath}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "Campaign: icewind") {
if decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Campaign != "icewind" {
t.Fatalf("stdout = %q, want selected campaign", stdout.String())
}
}
func TestExecuteRejectsCampaignIDAndCampaignFile(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign", "sample-campaign", "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "mutually exclusive") {
t.Fatalf("stderr = %q, want mutually exclusive error", stderr.String())
}
}
func TestExecuteInvalidCommand(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
@@ -486,22 +390,6 @@ func TestExecuteInvalidCommand(t *testing.T) {
}
}
func TestExecuteMissingCommand(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(nil, &stdout, &stderr)
if code == 0 {
t.Fatalf("exit code = 0, want non-zero")
}
if stdout.Len() != 0 {
t.Fatalf("stdout = %q, want empty", stdout.String())
}
if !strings.Contains(stderr.String(), "Usage: narratio") {
t.Fatalf("stderr = %q, want usage message", stderr.String())
}
}
func writeValidConfigFiles(t *testing.T, workspaceRoot string, transcribeURL ...string) (string, string, string) {
t.Helper()
@@ -591,9 +479,9 @@ inputs:
func materializePrepareResumeFixture(t *testing.T, pipelinePath, campaignPath, sessionPath string) {
t.Helper()
if err := RunStage(
if err := invokeNativeCommand(
context.Background(),
[]string{"prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
[]string{"run", "--stage", "prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
&bytes.Buffer{},
); err != nil {
t.Fatalf("materialize prepare resume fixture: %v", err)

View File

@@ -164,19 +164,19 @@ func TestNativeBundleInspectionAndExecutionAgree(t *testing.T) {
execution = cfg
return &RunSummary{SessionID: "2026-05-03"}, nil
}
runArgs := append([]string{"2026-05-03", "--session", filepath.Join(bundle, "session.local-audio.yml"), "--overlay", "testing"}, common...)
runArgs := append([]string{"2026-05-03", "--session-file", filepath.Join(bundle, "session.local-audio.yml"), "--overlay", "testing"}, common...)
var plan bytes.Buffer
planArgs := append(append([]string{}, runArgs...), "--from", "prepare", "--through", "prepare")
if err := Plan(context.Background(), planArgs, &plan); err != nil {
if err := invokeNativeCommand(context.Background(), append([]string{"session", "plan"}, planArgs...), &plan); err != nil {
t.Fatal(err)
}
if !strings.Contains(plan.String(), "prepare: run") {
if report := decodeNativeReport[sessionPlanReport](t, plan.Bytes()); len(report.Stages) != 1 || report.Stages[0].Name != "prepare" || report.Stages[0].Action != "run" {
t.Fatalf("unexpected plan: %s", plan.String())
}
if _, err := os.Stat(workspace); !os.IsNotExist(err) {
t.Fatalf("plan created workspace: %v", err)
}
if err := runWithPipelineExecutor(context.Background(), runArgs, io.Discard, executor); err != nil {
if err := invokeNativeCommandWith(context.Background(), append([]string{"run"}, runArgs...), io.Discard, executor, commandDependencies{}); err != nil {
t.Fatal(err)
}
if execution == nil || execution.Session.Inputs.AudioDir != filepath.Join(bundle, "audio") || execution.Campaign.Inputs.PartyFile != filepath.Join(bundle, "campaigns", "sample-campaign", "party.yml") {

View File

@@ -15,57 +15,8 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
"go.maximumdirect.net/commandkit"
)
type pipelineCampaignConfig = config.LoadedPipelineCampaign
type commandConfig struct {
Config *config.Config
cleanup func() error
}
func (c *commandConfig) Close() error {
if c == nil || c.cleanup == nil {
return nil
}
cleanup := c.cleanup
c.cleanup = nil
return cleanup()
}
func retainedCommandConfig(cfg *config.Config) *commandConfig {
return &commandConfig{Config: cfg}
}
func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campaignFlag, campaignFileFlag, sessionFlag string, sessionOpts config.SessionLoadOptions, secretsDir *string, deps commandDependencies) (*commandConfig, error) {
selection := configurationSelection{}
for _, field := range []struct {
value string
target *commandkit.Supplied[string]
}{
{pipelineFlag, &selection.Config}, {campaignFlag, &selection.Campaign}, {campaignFileFlag, &selection.CampaignFile},
{sessionFlag, &selection.SessionFile}, {sessionOpts.SessionID, &selection.Session},
} {
if field.value != "" {
*field.target = commandkit.Supplied[string]{Value: field.value, Set: true}
}
}
if secretsDir != nil {
selection.SecretsDir = commandkit.Supplied[string]{Value: *secretsDir, Set: true}
}
if sessionOpts.Overlay != nil {
selection.Overlay = commandkit.Supplied[string]{Value: *sessionOpts.Overlay, Set: true}
}
acquisition := newConfigurationAcquisition(selection, deps)
acquisition.previousSessionID = sessionOpts.PreviousSessionID
cfg, err := acquisition.Domain(ctx)
if err != nil {
return nil, errors.Join(err, acquisition.Close())
}
return &commandConfig{Config: cfg, cleanup: acquisition.Close}, nil
}
type acquiredSession struct {
Config *config.SessionConfig
Path string
@@ -94,7 +45,7 @@ func loadRemoteSessionConfig(ctx context.Context, base *config.LoadedPipelineCam
sessionPrefix := artifacts.S3SessionPrefix(rootPrefix, config.CampaignID(base.Campaign), sessionID)
remoteKey := artifacts.S3SessionConfigKey(sessionPrefix)
partialCfg := &config.Config{Pipeline: base.Pipeline, PipelinePath: base.PipelinePath, Campaign: base.Campaign, CampaignPath: base.CampaignPath}
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, nil, deps)
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, deps)
if err != nil {
return nil, fmt.Errorf("remote session %q unavailable: %w", remoteKey, err)
}
@@ -150,32 +101,6 @@ func loadRemoteSessionConfig(ctx context.Context, base *config.LoadedPipelineCam
return loaded, nil
}
func loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, campaignFileFlag string, pipelineOpts config.PipelineLoadOptions, secretsDir *string, deps commandDependencies) (*pipelineCampaignConfig, error) {
deps = normalizeCommandDependencies(deps)
loadedPipelinePath, pipelineCfg, err := loadPipelineConfigWithDependencies(pipelineFlag, pipelineOpts, secretsDir, deps)
if err != nil {
return nil, err
}
resolvedCampaignPath, err := resolveCampaignConfigPath(pipelineCfg, campaignFlag, campaignFileFlag)
if err != nil {
return nil, err
}
campaignCfg, err := config.LoadCampaign(resolvedCampaignPath)
if err != nil {
return nil, err
}
if selectedID := strings.TrimSpace(campaignFlag); selectedID != "" {
if got := config.CampaignID(campaignCfg); got != selectedID {
return nil, &config.SessionIdentityError{Source: resolvedCampaignPath, Field: "campaign_id", Expected: selectedID, Actual: got}
}
}
loaded, err := config.LoadPipelineCampaign(loadedPipelinePath, pipelineCfg, resolvedCampaignPath, campaignCfg)
if err != nil {
return nil, err
}
return &loaded, nil
}
func loadPipelineConfigWithDependencies(pipelineFlag string, opts config.PipelineLoadOptions, secretsDir *string, deps commandDependencies) (string, *config.PipelineConfig, error) {
deps = normalizeCommandDependencies(deps)
if !deps.secretsBootstrap.selectionSet {

View File

@@ -35,7 +35,8 @@ func TestLoadCommandConfigRetainsInitiallyLoadedPipeline(t *testing.T) {
},
}
loaded, err := loadCommandConfigWithDependencies(context.Background(), pipelinePath, "", campaignPath, sessionPath, config.SessionLoadOptions{SessionID: "2026-05-03"}, nil, deps)
loaded := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), SessionFile: suppliedSelection(sessionPath), Session: suppliedSelection("2026-05-03")}, deps)
resolved, err := loaded.Domain(context.Background())
if err != nil {
t.Fatalf("loadCommandConfig() error = %v", err)
}
@@ -44,7 +45,7 @@ func TestLoadCommandConfigRetainsInitiallyLoadedPipeline(t *testing.T) {
if loadCalls != 1 {
t.Fatalf("pipeline load calls = %d, want 1", loadCalls)
}
if got := loaded.Config.Pipeline.Workspace.Root; got != workspaceRoot {
if got := resolved.Pipeline.Workspace.Root; got != workspaceRoot {
t.Fatalf("resolved workspace root = %q, want originally loaded %q", got, workspaceRoot)
}
}

View File

@@ -1,8 +1,10 @@
package app
import (
"bytes"
"context"
"errors"
"io"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/ckconfig"
@@ -11,9 +13,10 @@ import (
)
type configurationInspectionState struct {
deps commandDependencies
independent bool
acquisitions map[configurationSelection]*configurationAcquisition
pendingDiagnostics *bytes.Buffer
deps commandDependencies
independent bool
acquisitions map[configurationSelection]*configurationAcquisition
}
func (s *configurationInspectionState) acquisition(selection ckconfig.Selection, input configurationSelection) *configurationAcquisition {
@@ -70,18 +73,28 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
scopes := []ckconfig.Scope[configurationSelection, *configurationInspectionState]{app, campaign, session, party}
// Comparison sides share bootstrap services, but always load their own results.
for index := range scopes {
acquire := scopes[index].Acquire
scopes[index].Acquire = func(ctx context.Context, request ckconfig.Request, input configurationSelection, state *configurationInspectionState) (commandkit.Acquired[ckconfig.View], error) {
result, err := acquire(ctx, request, input, state)
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
return result, err
}
compare := scopes[index].Compare
scopes[index].Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[configurationSelection], state *configurationInspectionState) (commandkit.Acquired[[]configkit.Difference], error) {
separate := *state
separate.independent = true
return compare(ctx, request, &separate)
result, err := compare(ctx, request, &separate)
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
return result, err
}
}
return ckconfig.Command(ckconfig.Suite[configurationSelection, *configurationInspectionState]{
NewState: func() *configurationInspectionState {
invocationDeps := normalizeCommandDependencies(deps)
invocationDeps.secretsBootstrap = &secretsBootstrap{}
return &configurationInspectionState{deps: invocationDeps, acquisitions: make(map[configurationSelection]*configurationAcquisition)}
pending := &bytes.Buffer{}
invocationDeps.diagnostics = pending
return &configurationInspectionState{deps: invocationDeps, pendingDiagnostics: pending, acquisitions: make(map[configurationSelection]*configurationAcquisition)}
},
CheckInput: func(_ string, selection ckconfig.Selection, input configurationSelection) error {
input.Config, input.Overlay = selection.Config, selection.Overlay
@@ -98,3 +111,12 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
Scopes: scopes,
})
}
func (s *configurationInspectionState) bootstrapDiagnostics() []commandkit.Diagnostic {
if s.pendingDiagnostics == nil || s.pendingDiagnostics.Len() == 0 {
return nil
}
text := s.pendingDiagnostics.String()
s.pendingDiagnostics.Reset()
return []commandkit.Diagnostic{{Severity: commandkit.SeverityInfo, Write: func(w io.Writer) error { _, err := io.WriteString(w, text); return err }}}
}

View File

@@ -86,9 +86,9 @@ func executionCommandSpec(name, summary string, regenerate bool) commandkit.Comm
}
if !regenerate {
flags = append(flags,
commandkit.ValueFlag(commandkit.FlagMeta{Name: "stage", Usage: "Select one canonical pipeline stage."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Stage = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "from", Usage: "First pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).From = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "through", Usage: "Last pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Through = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "stage", Completion: stageCompletion(), Usage: "Select one canonical pipeline stage."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Stage = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "from", Completion: stageCompletion(), Usage: "First pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).From = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "through", Completion: stageCompletion(), Usage: "Last pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Through = value }),
commandkit.BoolFlag(commandkit.FlagMeta{Name: "force", Usage: "Rerun selected stages."}, false, func(input **executionInput, value commandkit.Supplied[bool]) { (*input).Force = value }),
)
}
@@ -144,3 +144,12 @@ func executeSelectedPipeline(ctx context.Context, input *executionInput, deps co
}
return &runReport{Campaign: config.CampaignID(cfg.Campaign), SessionID: summary.SessionID, RunID: summary.RunID, Stages: input.plan.Names(), Executed: summary.Executed, Skipped: summary.Skipped, ArtifactTargets: effective.Keys(), StageResults: summary.StageResults, AvailableArtifacts: summary.AvailableArtifacts, Complete: err == nil}, err
}
func stageCompletion() commandkit.CompletionHint {
names := canonicalStageNames()
choices := make([]commandkit.CompletionChoice, 0, len(names))
for _, name := range names {
choices = append(choices, commandkit.CompletionChoice{Value: name})
}
return commandkit.CompleteChoices(choices...)
}

View File

@@ -240,6 +240,10 @@ characters:
t.Fatalf("ParseParty(changed party) error = %v", err)
}
cfg.Party.Canonical = partyDocument
cfg.Party.Players, err = partyDocument.PlayersYAML()
if err != nil {
t.Fatal(err)
}
prepared := loadLifecycleManifest(t, cfg)
prepare, err := stage.Select("prepare")
@@ -297,6 +301,10 @@ characters:
t.Fatalf("ParseParty(canonical party) error = %v", err)
}
cfg.Party.Canonical = partyDocument
cfg.Party.Players, err = partyDocument.PlayersYAML()
if err != nil {
t.Fatal(err)
}
if _, err := executeStages(context.Background(), cfg, prepareExtractLifecyclePlan(t), RunOptions{Env: env}); err != nil {
t.Fatalf("executeStages() error = %v", err)
@@ -438,6 +446,10 @@ characters:
t.Fatalf("ParseParty(changed party) error = %v", err)
}
cfg.Party.Canonical = partyDocument
cfg.Party.Players, err = partyDocument.PlayersYAML()
if err != nil {
t.Fatal(err)
}
persisted := loadLifecycleManifest(t, cfg)
prepare, err := stage.Select("prepare")
if err != nil {

View File

@@ -87,7 +87,7 @@ func executeLockCommand(ctx context.Context, input *lockCommandInput, operation
if err := config.Validate(cfg); err != nil {
return report, err
}
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, deps)
if err != nil {
return report, err
}

View File

@@ -0,0 +1,120 @@
package app
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"testing"
"time"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"go.maximumdirect.net/commandkit"
)
// Invoke the same installed tree as the process, with optional external seams.
func invokeNativeCommand(ctx context.Context, args []string, out io.Writer) error {
return invokeNativeCommandWith(ctx, args, out, executePlan, commandDependencies{})
}
func invokeNativeCommandWith(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
application, err := newApplication(deps, executor)
if err != nil {
return err
}
outcome := application.Execute(ctx, args, commandkit.Streams{Out: out, Err: io.Discard})
if outcome.Status == 0 {
return nil
}
if outcome.Cause != nil {
return outcome.Cause
}
return fmt.Errorf("command failed with status %d: %s", outcome.Status, outcome.Explanation.Message)
}
func decodeNativeReport[T any](t *testing.T, body []byte) T {
t.Helper()
value, err := commandkit.DecodeYAMLReport(bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
var result T
if err := json.Unmarshal(data, &result); err != nil {
t.Fatal(err)
}
return result
}
func runCountsMatch(t *testing.T, body []byte, executed, skipped int, stageName string) bool {
t.Helper()
report := decodeNativeReport[runReport](t, body)
return report.Complete && len(report.Executed) == executed && len(report.Skipped) == skipped && (stageName == "" || (len(report.Stages) == 1 && report.Stages[0] == stageName))
}
func markFixturePrerequisites(t *testing.T, m *manifest.Manifest, selected string) {
t.Helper()
plan, err := BuildBoundedPlan(selected, selected)
if err != nil {
t.Fatal(err)
}
for _, name := range plan.PrefixNames() {
if m.Stages[name] == nil {
m.MarkStageSucceeded(name, time.Now().UTC(), nil)
}
}
}
func seedInvocationPrerequisites(t *testing.T, root, selected string) {
t.Helper()
store := &manifest.LocalStore{}
path := artifacts.SessionManifestPathForCampaign(root, "sample-campaign", "2026-05-03")
m, _, err := loadManifestAtPathIfPresent(context.Background(), store, path)
if err != nil {
t.Fatal(err)
}
if m == nil {
m = manifest.New("2026-05-03", time.Now().UTC())
m.Campaign = "sample-campaign"
}
markFixturePrerequisites(t, m, selected)
if err := store.Save(context.Background(), path, m); err != nil {
t.Fatal(err)
}
}
func findingMatches(findings []finding, severity, category, message string) bool {
for _, item := range findings {
if (severity == "" || item.Severity == severity) && item.Category == category && strings.Contains(item.Message, message) {
return true
}
}
return false
}
func publishedReportOutput(t *testing.T, body []byte, source string) sessionPublishedOutput {
t.Helper()
report := decodeNativeReport[sessionStateReport](t, body)
if report.Remote != nil {
for _, item := range report.Remote.Outputs {
if item.Source == source {
return item
}
}
}
t.Fatalf("published source %q missing from report: %#v", source, report)
return sessionPublishedOutput{}
}
func localReportArtifact(t *testing.T, body []byte, source string) sessionArtifactState {
t.Helper()
report := decodeNativeReport[sessionStateReport](t, body)
for _, item := range report.Artifacts {
if item.Source == source {
return item
}
}
t.Fatalf("artifact %q missing from report: %#v", source, report)
return sessionArtifactState{}
}

View File

@@ -3,13 +3,12 @@ package app
import (
"context"
"fmt"
"log/slog"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/config"
)
func newCommandObjectStoreWithDependencies(ctx context.Context, cfg *config.Config, logger *slog.Logger, deps commandDependencies) (storage.ObjectStore, error) {
func newCommandObjectStoreWithDependencies(ctx context.Context, cfg *config.Config, deps commandDependencies) (storage.ObjectStore, error) {
deps = normalizeCommandDependencies(deps)
return initializeObjectStoreBackend(ctx, cfg, deps.newObjectStore)
}

View File

@@ -16,7 +16,7 @@ func TestNewCommandObjectStoreFactoryErrorIsContextual(t *testing.T) {
return nil, errors.New("factory boom")
}}
_, err := newCommandObjectStoreWithDependencies(context.Background(), cfg, nil, deps)
_, err := newCommandObjectStoreWithDependencies(context.Background(), cfg, deps)
if err == nil {
t.Fatal("expected error, got nil")
}

View File

@@ -1,273 +0,0 @@
package app
import (
"context"
"fmt"
"io"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func buildHelperArtifactCatalog(cfg *config.Config, m *manifest.Manifest) (*artifacts.ArtifactCatalog, error) {
configured := artifacts.ConfiguredArtifactDefinitions(nil)
if cfg.Pipeline.Scriptorium != nil {
configured = artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())
}
extractionDefinitions := artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)
effective, err := artifacts.ResolveEffectiveArtifactSet(configured, nil)
if err != nil {
return nil, err
}
catalog, err := artifacts.BootstrapRuntimeCatalog(configured, effective, extractionDefinitions)
if err != nil {
return nil, err
}
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
if cfg.Pipeline.Notarius != nil && cfg.Pipeline.Notarius.Enabled {
catalog.HydrateExtractionArtifacts(paths, m, extractionDefinitions)
}
catalog.HydrateAnalyzeArtifacts(paths, m, configured)
return catalog, nil
}
func writeArtifactList(out io.Writer, cfg *config.Config, catalog *artifacts.ArtifactCatalog, locks []artifacts.RemoteLock, publishedRemoteState map[string]string) {
lockSet := remoteLockSourceSet(locks)
fmt.Fprintln(out, "Built-in:")
for _, transcript := range artifacts.RuntimeTranscriptArtifacts() {
writeArtifactLine(out, transcript.SourceID, lockSet)
}
writeArtifactLine(out, artifacts.ArtifactBoundsSession, lockSet)
fmt.Fprintln(out, "Configured:")
origins := config.ArtifactFamilies(cfg.ArtifactCatalog).Members
for _, entry := range catalog.ListConfigured() {
state := "unavailable"
if entry.Available {
state = "available"
}
writeConfiguredArtifactLine(out, entry.SourceID, state, entry.Provenance, lockSet, origins)
}
fmt.Fprintln(out, "Extraction:")
for _, entry := range catalog.ListExtraction() {
state := "unavailable"
if entry.Available {
state = "available"
}
writeExtractionArtifactLine(out, entry.SourceID, state, entry.Provenance, lockSet)
}
fmt.Fprintln(out, "Previous-session:")
if effective, err := resolveEffectiveArtifacts(cfg, nil); err == nil {
for _, req := range artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective) {
fmt.Fprintf(out, "- %s required=%t\n", artifactpolicy.PreviousSessionSourceID(req.Name), req.Required)
}
}
fmt.Fprintln(out, "Published:")
for _, rule := range cfg.PublicationOutputs() {
writePublishedOutputLine(out, rule, catalog, lockSet, publishedRemoteState)
}
}
func writeConfiguredArtifactLine(out io.Writer, source, state, provenance string, lockSet map[string]struct{}, origins map[string]config.ArtifactFamilyMemberOrigin) {
parts := []string{source, "planned", state}
if key, ok := artifactpolicy.ParseConfiguredSource(source); ok {
if origin, family := origins[key]; family {
parts = append(parts, "family="+origin.Family, "character_id="+origin.CharacterID)
}
}
if strings.TrimSpace(provenance) != "" {
parts = append(parts, "provenance="+strings.TrimSpace(provenance))
}
if _, ok := lockSet[source]; ok {
parts = append(parts, "locked")
}
fmt.Fprintf(out, "- %s\n", strings.Join(parts, " "))
}
func writeExtractionArtifactLine(out io.Writer, source, state, provenance string, lockSet map[string]struct{}) {
parts := []string{source, "planned", state}
if strings.TrimSpace(provenance) != "" {
parts = append(parts, "provenance="+strings.TrimSpace(provenance))
}
if _, ok := lockSet[source]; ok {
parts = append(parts, "locked")
}
fmt.Fprintf(out, "- %s\n", strings.Join(parts, " "))
}
func writeArtifactLine(out io.Writer, source string, lockSet map[string]struct{}) {
parts := []string{source}
if _, ok := lockSet[source]; ok {
parts = append(parts, "locked")
}
fmt.Fprintf(out, "- %s\n", strings.Join(parts, " "))
}
func writePublishedOutputLine(out io.Writer, rule config.PublishOutputRule, catalog *artifacts.ArtifactCatalog, lockSet map[string]struct{}, remoteState map[string]string) {
source := strings.TrimSpace(rule.Source)
parts := []string{source}
if _, ok := lockSet[source]; ok {
parts = append(parts, "locked")
}
dest, showDest, err := helperPublishedOutputDest(rule, catalog)
if err != nil {
parts = append(parts, "remote=error")
fmt.Fprintf(out, "- %s\n", strings.Join(parts, " "))
return
}
if showDest {
parts = append(parts, "dest="+dest)
}
if state := remoteState[publishedOutputRemoteStateKey(source, dest)]; state != "" {
parts = append(parts, state)
}
fmt.Fprintf(out, "- %s\n", strings.Join(parts, " "))
}
func remoteLockSourceSet(locks []artifacts.RemoteLock) map[string]struct{} {
out := make(map[string]struct{}, len(locks))
for _, lock := range locks {
out[lock.Source] = struct{}{}
}
return out
}
func unresolvedRemotePublishedOutputAvailability(cfg *config.Config, catalog *artifacts.ArtifactCatalog, state string) map[string]string {
out := map[string]string{}
for _, rule := range cfg.PublicationOutputs() {
source := strings.TrimSpace(rule.Source)
dest, _, err := helperPublishedOutputDest(rule, catalog)
if err != nil {
out[publishedOutputRemoteStateKey(source, "")] = "remote=error"
continue
}
out[publishedOutputRemoteStateKey(source, dest)] = state
}
return out
}
func remotePublishedOutputAvailabilityForCurrent(
ctx context.Context,
cfg *config.Config,
store storage.ObjectStore,
catalog *artifacts.ArtifactCatalog,
current *RemoteCurrentState,
) map[string]string {
state := "current"
if current == nil || current.Commit == nil {
state = "missing"
}
outputs, _ := inspectPublishedOutputs(ctx, cfg, store, catalog, current, nil, false, state)
out := map[string]string{}
for _, output := range outputs {
value := ""
if output.Outcome != "" {
value = "outcome=" + output.Outcome + " "
}
value += "remote=" + output.State
if output.State == "published" && current != nil && (output.Outcome == "carried_locked" || output.OriginRun != current.RunID) {
value += " declared_dest=" + output.DeclaredDestination + " declared_key=" + output.DeclaredKey + " origin_run=" + output.OriginRun
}
out[publishedOutputRemoteStateKey(output.Source, output.Destination)] = value
}
return out
}
func committedPublishOutputOutcomes(m *manifest.Manifest) map[string]string {
out := map[string]string{}
if m == nil || m.Stages["publish"] == nil {
return out
}
raw := m.Stages["publish"].Metadata["publish_output_outcomes"]
appendOutcome := func(item map[string]any) {
source, sourceOK := item["source"].(string)
status, statusOK := item["status"].(string)
if sourceOK && statusOK && isPublishOutputOutcome(status) {
out[source] = status
}
}
switch items := raw.(type) {
case []any:
for _, rawItem := range items {
if item, ok := rawItem.(map[string]any); ok {
appendOutcome(item)
}
}
case []map[string]any:
for _, item := range items {
appendOutcome(item)
}
}
return out
}
func isPublishOutputOutcome(status string) bool {
switch status {
case "published", "carried_locked", "locked_unpublished", "optional_missing", "unselected":
return true
default:
return false
}
}
func committedPublishedOutput(commit *artifacts.RemoteCommitManifest, source string) (artifacts.RemoteArtifact, bool) {
if commit == nil {
return artifacts.RemoteArtifact{}, false
}
for _, artifact := range commit.Artifacts {
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && artifact.Source == source {
return artifact, true
}
}
return artifacts.RemoteArtifact{}, false
}
func helperPublishedOutputDest(rule config.PublishOutputRule, catalog *artifacts.ArtifactCatalog) (string, bool, error) {
source := strings.TrimSpace(rule.Source)
normalized, err := artifactpolicy.ResolvePublishedDestinationWithExtractions(
source,
rule.Dest,
helperConfiguredOutputPathMap(catalog),
helperExtractionOutputSet(catalog),
)
if err != nil {
return "", false, err
}
entry, ok := catalog.Lookup(source)
showDest := !ok || strings.TrimSpace(entry.CanonicalRelPath) != normalized
return normalized, showDest, nil
}
func helperExtractionOutputSet(catalog *artifacts.ArtifactCatalog) map[string]struct{} {
out := map[string]struct{}{}
if catalog == nil {
return out
}
for _, entry := range catalog.ListExtraction() {
if strings.TrimSpace(entry.ExtractionKey) != "" {
out[entry.ExtractionKey] = struct{}{}
}
}
return out
}
func helperConfiguredOutputPathMap(catalog *artifacts.ArtifactCatalog) map[string]string {
out := map[string]string{}
if catalog == nil {
return out
}
for _, entry := range catalog.ListConfigured() {
if strings.TrimSpace(entry.ConfiguredKey) == "" {
continue
}
out[entry.ConfiguredKey] = strings.TrimSpace(entry.CanonicalRelPath)
}
return out
}
func publishedOutputRemoteStateKey(source, dest string) string {
return strings.TrimSpace(source) + "\x00" + strings.TrimSpace(dest)
}

View File

@@ -1,43 +0,0 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"strings"
"go.maximumdirect.net/commandkit"
)
// ArtifactsList lists effective artifact sources.
func ArtifactsList(ctx context.Context, args []string, out io.Writer) error {
return artifactsListWithDependencies(ctx, args, out, commandDependencies{})
}
func artifactsListWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
fs := flag.NewFlagSet("artifacts list", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
var remote bool
addCommonConfigFlags(fs, &flags)
fs.BoolVar(&remote, "remote", false, "inspect remote publish availability")
if err := parseSessionAwareFlags("artifacts list", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("artifacts list: session_id is required")
}
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return err
}
defer func() { _ = loaded.Close() }()
report, err := inspectSessionState(ctx, loaded.Config, false, remote, deps)
if report != nil {
err = errors.Join(err, commandkit.RenderReport(out, commandkit.Report{Value: report}, ""))
}
return err
}

View File

@@ -3,13 +3,12 @@ package app
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
@@ -20,32 +19,6 @@ type finding struct {
Message string `json:"message"`
}
type findingError struct {
count int
}
func (e findingError) Error() string {
return fmt.Sprintf("%d validation error(s)", e.count)
}
func renderFindings(out io.Writer, campaign, sessionID string, findings []finding) error {
if campaign != "" || sessionID != "" {
fmt.Fprintf(out, "Campaign: %s\n", campaign)
fmt.Fprintf(out, "Session: %s\n\n", sessionID)
}
errorsCount := 0
for _, f := range findings {
if f.Severity == "ERROR" {
errorsCount++
}
fmt.Fprintf(out, "%-5s %-10s %s\n", f.Severity, f.Category, f.Message)
}
if errorsCount > 0 {
return findingError{count: errorsCount}
}
return nil
}
func okFinding(category, msg string) finding { return finding{"OK", category, msg} }
func infoFinding(category, msg string) finding { return finding{"INFO", category, msg} }
func warnFinding(category, msg string) finding { return finding{"WARN", category, msg} }
@@ -62,23 +35,6 @@ func sessionSourceSummary(cfg *config.Config) string {
return source + " " + cfg.SessionPath
}
func validateStableInputFindings(cfg *config.Config) []finding {
checks := inspectStableInputs(cfg)
out := make([]finding, 0, len(checks))
for _, check := range checks {
if check.Err != nil {
msg := check.Name + ": " + check.Err.Error()
if strings.TrimSpace(check.Path) != "" {
msg = fmt.Sprintf("%s missing: %v", check.Name, check.Err)
}
out = append(out, errorFinding("inputs", msg))
continue
}
out = append(out, okFinding("inputs", check.Name+": "+check.Path))
}
return out
}
func resolveHelperConfigRelativePath(input config.ResolvedInputFile) (string, error) {
if strings.TrimSpace(input.ConfigPath) == "" {
return "", fmt.Errorf("source config path is required")
@@ -93,25 +49,6 @@ func resolveHelperConfigRelativePath(input config.ResolvedInputFile) (string, er
return filepath.Clean(filepath.Join(filepath.Dir(input.ConfigPath), path)), nil
}
func validateLocalAudioFindings(cfg *config.Config) []finding {
check := inspectLocalAudioPresence(cfg)
if !check.Checked {
return nil
}
if check.Err != nil {
return []finding{errorFinding("audio", check.Err.Error())}
}
return []finding{okFinding("audio", fmt.Sprintf("%d local audio file(s)", len(check.Paths)))}
}
func validateRemoteAudioFinding(ctx context.Context, cfg *config.Config, store storage.ObjectStore) finding {
check := inspectRemoteAudioPresence(ctx, cfg, store)
if check.Err != nil {
return errorFinding("audio", check.Err.Error())
}
return okFinding("audio", fmt.Sprintf("%d remote .flac object(s)", len(check.Keys)))
}
func loadLocalManifest(ctx context.Context, path string) (*manifest.Manifest, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
@@ -122,19 +59,3 @@ func loadLocalManifest(ctx context.Context, path string) (*manifest.Manifest, er
store := &manifest.LocalStore{}
return store.Load(ctx, path)
}
func writeStageStatuses(out io.Writer, m *manifest.Manifest) {
if m == nil || len(m.Stages) == 0 {
fmt.Fprintln(out, "stages: no stages recorded")
return
}
fmt.Fprintln(out, "stages:")
names := make([]string, 0, len(m.Stages))
for name := range m.Stages {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
fmt.Fprintf(out, "- %s: %s\n", name, m.Stages[name].Status)
}
}

View File

@@ -2,167 +2,22 @@ package app
import (
"context"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
type commonConfigFlags struct {
pipelinePath string
campaignPath string
campaignFilePath string
sessionPath string
sessionID string
previousSessionID string
overlay singletonStringFlag
secretsDir singletonStringFlag
}
func addCommonConfigFlags(fs *flag.FlagSet, flags *commonConfigFlags) {
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")
fs.StringVar(&flags.sessionPath, "session", "", "path to session.yml")
fs.StringVar(&flags.sessionID, "session-id", "", "session identifier")
fs.StringVar(&flags.previousSessionID, "previous-session-id", "", "expected previous session identifier")
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)")
}
func (f commonConfigFlags) sessionOptions() config.SessionLoadOptions {
return config.SessionLoadOptions{
SessionID: f.sessionID,
PreviousSessionID: f.previousSessionID,
Overlay: f.overlay.pointer(),
}
}
// Session dispatches session helper subcommands.
func Session(ctx context.Context, args []string, out io.Writer) error {
return sessionWithDependencies(ctx, args, out, commandDependencies{})
}
func sessionWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
if len(args) == 0 {
return fmt.Errorf("session: expected subcommand: init|validate|status|plan|restore|artifacts|locks")
}
switch args[0] {
case "init":
return sessionInitWithDependencies(ctx, args[1:], out, deps)
case "validate":
return sessionValidateWithDependencies(ctx, args[1:], out, deps)
case "status":
return statusWithDependencies(ctx, args[1:], out, deps)
case "plan":
return planWithDependencies(ctx, args[1:], out, deps)
case "restore":
return restoreWithDependencies(ctx, args[1:], out, deps)
case "artifacts":
return artifactsListWithDependencies(ctx, args[1:], out, deps)
case "locks":
return sessionLocksWithDependencies(ctx, args[1:], out, deps)
default:
return fmt.Errorf("session: unknown subcommand %q", args[0])
}
}
// SessionLocks dispatches session-oriented publish lock list and mutation
// helpers.
func SessionLocks(ctx context.Context, args []string, out io.Writer) error {
return sessionLocksWithDependencies(ctx, args, out, commandDependencies{})
}
func sessionLocksWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
if len(args) > 0 && !isCLIFlagToken(args[0]) {
switch args[0] {
case "add":
return locksAddWithDependencies(ctx, args[1:], out, deps)
case "remove":
return locksRemoveWithDependencies(ctx, args[1:], out, deps)
}
}
return locksListWithDependencies(ctx, args, out, deps)
}
// Artifacts dispatches artifact helper subcommands.
func Artifacts(ctx context.Context, args []string, out io.Writer) error {
return artifactsWithDependencies(ctx, args, out, commandDependencies{})
}
func artifactsWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
if len(args) == 0 {
return fmt.Errorf("artifacts: expected subcommand: list")
}
switch args[0] {
case "list":
return artifactsListWithDependencies(ctx, args[1:], out, deps)
default:
return fmt.Errorf("artifacts: unknown subcommand %q", args[0])
}
}
func loadHelperContextWithDependencies(ctx context.Context, flags commonConfigFlags, needStore bool, deps commandDependencies) (*config.Config, storage.ObjectStore, []artifacts.RemoteLock, *manifest.Manifest, func(), error) {
deps = normalizeCommandDependencies(deps)
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return nil, nil, nil, nil, nil, err
}
release := true
defer func() {
if release {
_ = loaded.Close()
}
}()
cfg := loaded.Config
if err := config.Validate(cfg); err != nil {
return nil, nil, nil, nil, nil, err
}
var store storage.ObjectStore
if needStore {
store, err = newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
if err != nil {
return nil, nil, nil, nil, nil, err
}
} else {
store, _ = objectStoreIfConfiguredWithDependencies(ctx, cfg, deps)
}
var locks []artifacts.RemoteLock
if store != nil {
sessionPrefix, prefixErr := artifacts.ResolvePublishSessionPrefix(cfg, nil)
if prefixErr != nil {
return nil, nil, nil, nil, nil, prefixErr
}
locks, err = artifacts.ListRemoteLocks(ctx, store, sessionPrefix)
if err != nil {
return nil, nil, nil, nil, nil, err
}
}
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
m, err := loadLocalManifest(ctx, paths.ManifestPath)
if err != nil {
return nil, nil, nil, nil, nil, err
}
release = false
return cfg, store, locks, m, func() { _ = loaded.Close() }, nil
}
func objectStoreIfConfiguredWithDependencies(ctx context.Context, cfg *config.Config, deps commandDependencies) (storage.ObjectStore, error) {
if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Storage.S3 == nil || strings.TrimSpace(cfg.Pipeline.Storage.S3.Bucket) == "" {
return nil, nil
}
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, deps)
if err != nil {
return nil, err
}

View File

@@ -158,7 +158,7 @@ func TestExecuteSessionInitRequiresSessionID(t *testing.T) {
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "session init: session_id is required") {
if code != 2 {
t.Fatalf("stderr = %q, want session-id required error", stderr.String())
}
}
@@ -454,14 +454,14 @@ inputs:
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithDependencies([]string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--secrets-dir", secretsDir}, &stdout, &stderr, deps, executePlan)
code := executeWithDependencies([]string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--secrets-dir", secretsDir}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "OK audio") {
if !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "OK", "audio", "") {
t.Fatalf("stdout = %q, want OK audio", stdout.String())
}
if !strings.Contains(stdout.String(), "OK inputs players:") || !strings.Contains(stdout.String(), "OK inputs party:") {
if !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "OK", "inputs", "players:") || !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "OK", "inputs", "party:") {
t.Fatalf("stdout = %q, want players and party input findings", stdout.String())
}
}
@@ -486,8 +486,7 @@ func TestOperatorCommandsReportConfiguredSpellCatalog(t *testing.T) {
commonArgs := []string{
"2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}
var stdout bytes.Buffer
@@ -496,7 +495,7 @@ func TestOperatorCommandsReportConfiguredSpellCatalog(t *testing.T) {
if code := executeWithDependencies(validateArgs, &stdout, &stderr, deps, executePlan); code != 0 {
t.Fatalf("validate exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "OK inputs spell_catalog: "+spellPath) {
if !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "OK", "inputs", spellPath) {
t.Fatalf("validate stdout = %q, want spell catalog finding", stdout.String())
}
@@ -506,7 +505,7 @@ func TestOperatorCommandsReportConfiguredSpellCatalog(t *testing.T) {
if code := executeWithDependencies(statusArgs, &stdout, &stderr, deps, executePlan); code != 0 {
t.Fatalf("status exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), "Stable input spell_catalog: "+spellPath) {
if !findingMatches(decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Findings, "INFO", "inputs", spellPath) {
t.Fatalf("status stdout = %q, want spell catalog inventory", stdout.String())
}
@@ -518,7 +517,7 @@ func TestOperatorCommandsReportConfiguredSpellCatalog(t *testing.T) {
if code := executeWithDependencies(validateArgs, &stdout, &stderr, deps, executePlan); code == 0 {
t.Fatalf("validate missing spell catalog exit code = 0; stdout=%q", stdout.String())
}
if !strings.Contains(stdout.String(), "ERROR inputs spell_catalog missing:") {
if !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "ERROR", "inputs", "spell_catalog") {
t.Fatalf("validate stdout = %q, want missing spell catalog finding", stdout.String())
}
}
@@ -535,8 +534,7 @@ func TestExecuteLocksAddListAndRemoveUseIndependentObjects(t *testing.T) {
code := executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--reason", "manual edit",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
@@ -558,15 +556,14 @@ func TestExecuteLocksAddListAndRemoveUseIndependentObjects(t *testing.T) {
stdout.Reset()
stderr.Reset()
code = executeWithDependencies([]string{
"session", "locks", "2026-05-03",
"session", "locks", "list", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("locks list exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "- narratio.transcript.final_trimmed published reason=manual edit created_at=") {
if len(decodeNativeReport[lockCommandReport](t, stdout.Bytes()).Locks) != 1 || decodeNativeReport[lockCommandReport](t, stdout.Bytes()).Locks[0].Reason != "manual edit" {
t.Fatalf("stdout = %q, want remote lock", stdout.String())
}
@@ -575,8 +572,7 @@ func TestExecuteLocksAddListAndRemoveUseIndependentObjects(t *testing.T) {
code = executeWithDependencies([]string{
"session", "locks", "remove", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("locks remove exit code = %d, want 0; stderr=%q", code, stderr.String())
@@ -589,10 +585,9 @@ func TestExecuteLocksAddListAndRemoveUseIndependentObjects(t *testing.T) {
code = executeWithDependencies([]string{
"session", "locks", "remove", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 || !strings.Contains(stdout.String(), "was not locked") {
if code != 0 || decodeNativeReport[lockCommandReport](t, stdout.Bytes()).Result != "not_locked" {
t.Fatalf("idempotent remove exit code = %d, stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if storeInitCalls != 4 {
@@ -609,17 +604,16 @@ func TestExecuteLocksListIsSortedAndRejectsMalformedObjects(t *testing.T) {
sessionPrefix := artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-05-03")
seedOperatorRemoteLock(t, fake, sessionPrefix, "narratio.transcript.final_trimmed", "second")
seedOperatorRemoteLock(t, fake, sessionPrefix, "narratio.transcript.final", "first")
args := []string{"session", "locks", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}
args := []string{"session", "locks", "list", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}
var stdout bytes.Buffer
var stderr bytes.Buffer
if code := executeWithDependencies(args, &stdout, &stderr, deps, executePlan); code != 0 {
t.Fatalf("locks list exit code = %d; stderr=%q", code, stderr.String())
}
first := strings.Index(stdout.String(), "narratio.transcript.final ")
second := strings.Index(stdout.String(), "narratio.transcript.final_trimmed ")
if first < 0 || second < 0 || first >= second {
t.Fatalf("locks list is not source sorted: %q", stdout.String())
locks := decodeNativeReport[lockCommandReport](t, stdout.Bytes()).Locks
if len(locks) != 2 || locks[0].Source != "narratio.transcript.final" || locks[1].Source != "narratio.transcript.final_trimmed" {
t.Fatalf("locks not source sorted: %#v", locks)
}
badKey, err := artifacts.RemoteLockKey(sessionPrefix, "narratio.transcript.base")
@@ -646,8 +640,7 @@ func TestExecuteLocksAddDuplicateRequiresForce(t *testing.T) {
code := executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--reason", "first",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
@@ -659,8 +652,7 @@ func TestExecuteLocksAddDuplicateRequiresForce(t *testing.T) {
code = executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--reason", "second",
}, &stdout, &stderr, deps, executePlan)
if code == 0 {
@@ -675,8 +667,7 @@ func TestExecuteLocksAddDuplicateRequiresForce(t *testing.T) {
code = executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--reason", "second",
"--force",
}, &stdout, &stderr, deps, executePlan)
@@ -720,38 +711,13 @@ func TestExecuteLocksForceReportsConcurrentReplacement(t *testing.T) {
var stderr bytes.Buffer
code := executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed", "--force",
"--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath,
"--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code == 0 || !strings.Contains(stderr.String(), "changed concurrently; retry") {
t.Fatalf("forced conflict exit code = %d, stderr=%q", code, stderr.String())
}
}
func TestExecuteLocksRequireSessionID(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{"list", []string{"session", "locks"}, "locks: session_id is required"},
{"add", []string{"session", "locks", "add", "narratio.transcript.final_trimmed"}, "locks add: expected session_id and source id"},
{"remove", []string{"session", "locks", "remove", "narratio.transcript.final_trimmed"}, "locks remove: expected session_id and source id"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest(tt.args, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), tt.want) {
t.Fatalf("stderr = %q, want %q", stderr.String(), tt.want)
}
})
}
}
func TestExecuteLocksMutationRejectsSessionIDMismatch(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
@@ -766,21 +732,19 @@ func TestExecuteLocksMutationRejectsSessionIDMismatch(t *testing.T) {
{
name: "add mismatch",
args: []string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--session-id", "2026-05-04",
"session", "locks", "add", "2026-05-04", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
},
},
{
name: "remove mismatch",
args: []string{
"session", "locks", "remove", "2026-05-03", "narratio.transcript.final_trimmed",
"--session-id", "2026-05-04",
"session", "locks", "remove", "2026-05-04", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
},
},
}
@@ -792,7 +756,7 @@ func TestExecuteLocksMutationRejectsSessionIDMismatch(t *testing.T) {
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "does not match expected session id") {
if !strings.Contains(stderr.String(), "session_id mismatch") {
t.Fatalf("stderr = %q, want session-id mismatch guidance", stderr.String())
}
})
@@ -809,7 +773,7 @@ func TestExecuteTopLevelLockAndUnlockAreRemoved(t *testing.T) {
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), `unknown command: "`+cmd+`"`) {
if code != 2 {
t.Fatalf("stderr = %q, want unknown command", stderr.String())
}
})
@@ -927,14 +891,13 @@ func TestExecuteArtifactsListRemoteReportsPublishedAvailability(t *testing.T) {
code := executeWithDependencies([]string{
"session", "artifacts", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--remote",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "narratio.transcript.final_trimmed remote=published") {
if publishedReportOutput(t, stdout.Bytes(), "narratio.transcript.final_trimmed").State != "published" {
t.Fatalf("stdout = %q, want published remote availability", stdout.String())
}
}
@@ -962,23 +925,18 @@ func TestExecuteArtifactsListReportsExtractionLifecycleWithoutPayload(t *testing
code := executeWithDependencies([]string{
"session", "artifacts", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--remote",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
out := stdout.String()
for _, want := range []string{
"Extraction:",
"narratio.extraction.encounters planned available provenance=manifest.current_extract_run",
"narratio.extraction.encounters dest=artifacts/encounters.json remote=published",
} {
if !strings.Contains(out, want) {
t.Fatalf("stdout = %q, want %q", out, want)
}
entry := localReportArtifact(t, stdout.Bytes(), "narratio.extraction.encounters")
if entry.Availability != "available" || entry.Provenance != artifacts.ArtifactProvenanceCurrentExtractManifest || publishedReportOutput(t, stdout.Bytes(), entry.Source).Destination != "artifacts/encounters.json" || publishedReportOutput(t, stdout.Bytes(), entry.Source).State != "published" {
t.Fatalf("extraction state=%#v", entry)
}
if strings.Contains(out, "DO_NOT_PRINT") {
t.Fatalf("operator output exposed extraction payload: %q", out)
}
@@ -991,13 +949,12 @@ func TestExecuteArtifactsListReportsExtractionLifecycleWithoutPayload(t *testing
code = executeWithDependencies([]string{
"session", "artifacts", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("unavailable exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "narratio.extraction.encounters planned unavailable") {
if localReportArtifact(t, stdout.Bytes(), "narratio.extraction.encounters").Availability != "unavailable" {
t.Fatalf("stdout = %q, want unavailable extraction state", stdout.String())
}
}
@@ -1027,28 +984,16 @@ func TestExecuteArtifactsListRemoteUsesPublishOutputDestinations(t *testing.T) {
code := executeWithDependencies([]string{
"session", "artifacts", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--remote",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
out := stdout.String()
for _, unwanted := range []string{
"narratio.transcript.final remote=missing",
"narratio.bounds.session remote=missing",
} {
if strings.Contains(out, unwanted) {
t.Fatalf("stdout = %q, did not want catalog remote marker %q", out, unwanted)
}
}
for _, want := range []string{
"narratio.transcript.final dest=transcripts/full.json remote=published",
"narratio.bounds.session dest=transcripts/bounds.json remote=published",
} {
if !strings.Contains(out, want) {
t.Fatalf("stdout = %q, want %q", out, want)
for source, dest := range map[string]string{"narratio.transcript.final": "transcripts/full.json", "narratio.bounds.session": "transcripts/bounds.json"} {
output := publishedReportOutput(t, stdout.Bytes(), source)
if output.Destination != dest || output.State != "published" {
t.Fatalf("published output=%#v", output)
}
}
}
@@ -1080,35 +1025,18 @@ func TestExecuteStatusReportsRemoteArtifactCatalog(t *testing.T) {
code := executeWithDependencies([]string{
"session", "status", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
out := stdout.String()
for _, want := range []string{
"Remote outputs:",
"Built-in:",
"Configured:",
"Previous-session:",
"Published:",
"narratio.transcript.final_trimmed locked",
"narratio.transcript.final_trimmed locked remote=published",
"narratio.transcript.final dest=transcripts/full.json remote=published",
"Stable input players:",
"Stable input party:",
} {
if !strings.Contains(out, want) {
t.Fatalf("stdout = %q, want %q", out, want)
}
}
if strings.Contains(out, "narratio.transcript.base remote=missing") {
t.Fatalf("stdout = %q, did not want catalog remote marker", out)
report := decodeNativeReport[sessionStateReport](t, stdout.Bytes())
if publishedReportOutput(t, stdout.Bytes(), "narratio.transcript.final_trimmed").State != "published" || !findingMatches(report.Findings, "INFO", "inputs", "players:") || !findingMatches(report.Findings, "INFO", "inputs", "party:") {
t.Fatalf("status=%#v", report)
}
}
func TestExecuteStatusReportsRemoteArtifactCatalogErrorsWithoutFailing(t *testing.T) {
func TestExecuteStatusReportsRemoteArtifactCatalogErrorsWithPartialData(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
addPublishOutputsToPipeline(t, pipelinePath, `
@@ -1126,18 +1054,14 @@ func TestExecuteStatusReportsRemoteArtifactCatalogErrorsWithoutFailing(t *testin
code := executeWithDependencies([]string{
"session", "status", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
if code != 1 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
out := stdout.String()
if !strings.Contains(out, "Remote publish: missing or unavailable:") {
t.Fatalf("stdout = %q, want remote publish unavailable state", out)
}
if !strings.Contains(out, "Remote outputs:") || !strings.Contains(out, "narratio.transcript.final_trimmed remote=error") {
t.Fatalf("stdout = %q, want remote output error state", out)
report := decodeNativeReport[sessionStateReport](t, stdout.Bytes())
if report.Remote == nil || report.Remote.State != "error" || publishedReportOutput(t, stdout.Bytes(), "narratio.transcript.final_trimmed").State != "error" {
t.Fatalf("partial status=%#v", report)
}
}
@@ -1153,18 +1077,17 @@ func TestExecuteStatusReportsMissingRemoteCurrentStateWithoutFailing(t *testing.
code := executeWithDependencies([]string{
"session", "status", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "Remote publish: missing or unavailable: remote current commit pointer missing") {
if decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Remote.State != "missing" {
t.Fatalf("stdout = %q, want missing remote current-state line", stdout.String())
}
}
func TestExecuteStatusReportsPreviousStateReadinessWithoutFailing(t *testing.T) {
func TestExecuteStatusReportsPreviousStateReadinessFailure(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
replaceInFileOrFatal(t, pipelinePath, "source: narratio.artifact.session_recap", "source: narratio.previous_session.artifact.session_recap")
@@ -1178,13 +1101,12 @@ func TestExecuteStatusReportsPreviousStateReadinessWithoutFailing(t *testing.T)
code := executeWithDependencies([]string{
"session", "status", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
if code != 1 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "Previous-session artifacts: unavailable: remote current commit pointer missing") {
if !findingMatches(decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Findings, "ERROR", "previous", "pointer missing") {
t.Fatalf("stdout = %q, want previous readiness unavailable line", stdout.String())
}
}
@@ -1208,13 +1130,12 @@ func TestExecuteStatusDetectsMissingRequiredPreviousArtifact(t *testing.T) {
code := executeWithDependencies([]string{
"session", "status", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
if code != 1 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), `Previous-session artifacts: unavailable: remote required previous-session artifact "session_recap" is not declared`) {
if !findingMatches(decodeNativeReport[sessionStateReport](t, stdout.Bytes()).Findings, "ERROR", "previous", "not declared") {
t.Fatalf("stdout = %q, want missing required previous artifact", stdout.String())
}
}
@@ -1233,19 +1154,18 @@ func TestExecuteSessionValidateReportsPreviousStateFindingAndReturnsFindingError
code := executeWithDependencies([]string{
"session", "validate", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stdout.String(), "ERROR previous") {
if !findingMatches(decodeNativeReport[sessionValidationReport](t, stdout.Bytes()).Findings, "ERROR", "previous", "") {
t.Fatalf("stdout = %q, want previous finding error", stdout.String())
}
if !strings.Contains(stdout.String(), "remote current commit pointer missing") {
t.Fatalf("stdout = %q, want missing commit pointer finding", stdout.String())
}
if !strings.Contains(stderr.String(), "validation error(s)") {
if !strings.Contains(stderr.String(), "remote current commit pointer missing") {
t.Fatalf("stderr = %q, want finding error summary", stderr.String())
}
}
@@ -1288,9 +1208,9 @@ func TestExecutePublishIgnoresRetiredSharedLockObject(t *testing.T) {
var stdout bytes.Buffer
var storeInitCalls int
err = runStageWithPipelineExecutor(
err = invokeNativeCommandWith(
context.Background(),
[]string{"publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"},
[]string{"run", "--stage", "publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force"},
&stdout,
func(ctx context.Context, cfg *config.Config, plan BoundedPlan, opts RunOptions) (*RunSummary, error) {
return executePlanWithDependencies(ctx, cfg, plan, opts, invocationDependencies{
@@ -1299,7 +1219,7 @@ func TestExecutePublishIgnoresRetiredSharedLockObject(t *testing.T) {
return fake, nil
},
})
},
}, commandDependencies{},
)
if err != nil {
t.Fatalf("runStageWithPipelineExecutor() error = %v", err)

View File

@@ -293,3 +293,12 @@ func requireInspectionFile(path, label string) error {
func isInspectionFlacPath(path string) bool {
return strings.EqualFold(filepath.Ext(strings.TrimSpace(path)), ".flac")
}
func previousRequirementSkipped(values []string, name string) bool {
for _, value := range values {
if value == name {
return true
}
}
return false
}

View File

@@ -1,164 +0,0 @@
package app
import (
"context"
"flag"
"fmt"
"io"
"strings"
"time"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
)
// LocksList lists independent publish locks.
func LocksList(ctx context.Context, args []string, out io.Writer) error {
return locksListWithDependencies(ctx, args, out, commandDependencies{})
}
func locksListWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
fs := flag.NewFlagSet("locks", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
addCommonConfigFlags(fs, &flags)
if err := parseSessionAwareFlags("locks", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("locks: session_id is required")
}
cfg, store, sessionPrefix, cleanup, err := loadLockCommandContextWithDependencies(ctx, flags, deps)
if err != nil {
return fmt.Errorf("locks: %w", err)
}
defer cleanup()
locks, err := artifacts.ListRemoteLocks(ctx, store, sessionPrefix)
if err != nil {
return fmt.Errorf("locks: %w", err)
}
writeLocks(out, cfg, locks)
return nil
}
// LocksAdd adds or updates one remote lock.
func LocksAdd(ctx context.Context, args []string, out io.Writer) error {
return locksAddWithDependencies(ctx, args, out, commandDependencies{})
}
func locksAddWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
fs := flag.NewFlagSet("locks add", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
var reason string
var force bool
addCommonConfigFlags(fs, &flags)
fs.StringVar(&reason, "reason", "", "lock reason")
fs.BoolVar(&force, "force", false, "update existing remote lock")
source, err := parseSessionIDAndOnePositionalArg("locks add", "source id", fs, args, &flags.sessionID)
if err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("locks add: session_id is required")
}
_, store, sessionPrefix, cleanup, err := loadLockCommandContextWithDependencies(ctx, flags, deps)
if err != nil {
return fmt.Errorf("locks add: %w", err)
}
defer cleanup()
_, err = applyRemoteLockMutation(ctx, store, sessionPrefix, "add", source, reason, force)
if err != nil {
return fmt.Errorf("locks add: %w", err)
}
_, err = fmt.Fprintf(out, "narratio session locks add: locked %s\n", source)
return err
}
// LocksRemove removes one remote lock.
func LocksRemove(ctx context.Context, args []string, out io.Writer) error {
return locksRemoveWithDependencies(ctx, args, out, commandDependencies{})
}
func locksRemoveWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
fs := flag.NewFlagSet("locks remove", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
addCommonConfigFlags(fs, &flags)
source, err := parseSessionIDAndOnePositionalArg("locks remove", "source id", fs, args, &flags.sessionID)
if err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("locks remove: session_id is required")
}
_, store, sessionPrefix, cleanup, err := loadLockCommandContextWithDependencies(ctx, flags, deps)
if err != nil {
return fmt.Errorf("locks remove: %w", err)
}
defer cleanup()
result, err := applyRemoteLockMutation(ctx, store, sessionPrefix, "remove", source, "", false)
if err != nil {
return fmt.Errorf("locks remove: %w", err)
}
if result == "not_locked" {
_, err = fmt.Fprintf(out, "narratio session locks remove: %s was not locked\n", source)
return err
}
_, err = fmt.Fprintf(out, "narratio session locks remove: unlocked %s\n", source)
return err
}
func loadLockCommandContextWithDependencies(ctx context.Context, flags commonConfigFlags, deps commandDependencies) (*config.Config, storage.ObjectStore, string, func(), error) {
deps = normalizeCommandDependencies(deps)
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return nil, nil, "", nil, err
}
release := true
defer func() {
if release {
_ = loaded.Close()
}
}()
cfg := loaded.Config
if err := config.Validate(cfg); err != nil {
return nil, nil, "", nil, err
}
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
if err != nil {
return nil, nil, "", nil, err
}
sessionPrefix, err := artifacts.ResolvePublishSessionPrefix(cfg, nil)
if err != nil {
return nil, nil, "", nil, err
}
release = false
return cfg, store, sessionPrefix, func() { _ = loaded.Close() }, nil
}
func writeLocks(out io.Writer, cfg *config.Config, locks []artifacts.RemoteLock) {
if len(locks) == 0 {
fmt.Fprintln(out, "Publish locks: none")
return
}
fmt.Fprintln(out, "Publish locks:")
published := map[string]config.PublishOutputRule{}
if cfg != nil && cfg.Pipeline != nil && cfg.Pipeline.Publish != nil {
for _, rule := range cfg.PublicationOutputs() {
published[strings.TrimSpace(rule.Source)] = rule
}
}
for _, lock := range locks {
promo := "not-published"
if _, ok := published[lock.Source]; ok {
promo = "published"
}
reason := strings.TrimSpace(lock.Reason)
if reason == "" {
reason = "(no reason)"
}
fmt.Fprintf(out, "- %s %s reason=%s created_at=%s\n", lock.Source, promo, reason, lock.CreatedAt.UTC().Format(time.RFC3339Nano))
}
}

View File

@@ -1,66 +1,17 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"go.maximumdirect.net/commandkit"
"gopkg.in/yaml.v3"
)
// SessionInit creates a local or remote session.yml skeleton.
func SessionInit(ctx context.Context, args []string, out io.Writer) error {
return sessionInitWithDependencies(ctx, args, out, commandDependencies{})
}
func sessionInitWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
fs := flag.NewFlagSet("session init", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var pipelinePath, campaignPath, campaignFilePath, sessionID, previousSessionID, date, title, output, audioS3Prefix, audioDir string
var overlay singletonStringFlag
var secretsDir singletonStringFlag
var remote, force bool
fs.StringVar(&pipelinePath, "config", "", "path to pipeline.yml (optional; defaults searched)")
fs.StringVar(&campaignPath, "campaign", "", "campaign ID")
fs.StringVar(&campaignFilePath, "campaign-file", "", "path to campaign.yml")
fs.StringVar(&sessionID, "session-id", "", "session identifier")
fs.StringVar(&previousSessionID, "previous-session-id", "", "previous session identifier")
fs.StringVar(&date, "date", "", "session date")
fs.StringVar(&title, "title", "", "session title")
fs.StringVar(&output, "output", "", "local output session.yml path")
fs.StringVar(&audioS3Prefix, "audio-s3-prefix", "", "session audio S3 prefix")
fs.StringVar(&audioDir, "audio-dir", "", "local audio directory")
overlay.name = "overlay"
fs.Var(&overlay, "overlay", "named pipeline overlay")
secretsDir.name = "secrets-dir"
fs.Var(&secretsDir, "secrets-dir", "secrets directory (optional; defaults to /usr/local/etc/narratio/secrets)")
fs.BoolVar(&remote, "remote", false, "write session.yml to S3 session prefix")
fs.BoolVar(&force, "force", false, "overwrite existing target")
if err := parseSessionAwareFlags("session init", fs, args, &sessionID); err != nil {
return err
}
request := &sessionInitializationInput{Selection: configurationSelection{
Config: commandkit.Supplied[string]{Value: pipelinePath, Set: pipelinePath != ""}, Campaign: commandkit.Supplied[string]{Value: campaignPath, Set: campaignPath != ""}, CampaignFile: commandkit.Supplied[string]{Value: campaignFilePath, Set: campaignFilePath != ""}, Session: commandkit.Supplied[string]{Value: sessionID, Set: sessionID != ""}, Overlay: commandkit.Supplied[string]{Value: overlay.value, Set: overlay.set}, SecretsDir: commandkit.Supplied[string]{Value: secretsDir.value, Set: secretsDir.set},
}, PreviousSessionID: previousSessionID, Date: date, Title: title, Output: output, AudioS3Prefix: audioS3Prefix, AudioDir: audioDir, Remote: remote, Force: force}
if err := checkSessionInitializationInput(request); err != nil {
return err
}
report, err := initializeSession(ctx, request, deps)
if report != nil {
err = errors.Join(err, commandkit.RenderReport(out, commandkit.Report{Value: report}, ""))
}
return err
}
func buildSessionYAML(campaign, sessionID, previousSessionID, date, title, audioS3Prefix, audioDir string) ([]byte, error) {
if strings.TrimSpace(date) == "" && regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(strings.TrimSpace(sessionID)) {
date = strings.TrimSpace(sessionID)

View File

@@ -35,8 +35,7 @@ inputs:
}
var out bytes.Buffer
err := SessionInit(context.Background(), []string{
"2026-05-03",
err := invokeNativeCommand(context.Background(), []string{"session", "init", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--output", filepath.Join(t.TempDir(), "session.yml"),

View File

@@ -1,49 +0,0 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"strings"
)
// SessionValidate performs a read-only session preflight.
func SessionValidate(ctx context.Context, args []string, out io.Writer) error {
return sessionValidateWithDependencies(ctx, args, out, commandDependencies{})
}
func sessionValidateWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
fs := flag.NewFlagSet("session validate", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
addCommonConfigFlags(fs, &flags)
if err := parseSessionAwareFlags("session validate", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("session validate: session_id is required")
}
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return renderFindings(out, "", "", []finding{errorFinding("config", err.Error())})
}
defer func() { _ = loaded.Close() }()
report, err := inspectSessionValidation(ctx, loaded.Config, deps)
if report == nil {
return err
}
return errors.Join(err, renderFindings(out, report.Campaign, report.SessionID, report.Findings))
}
func previousRequirementSkipped(values []string, name string) bool {
for _, value := range values {
if value == name {
return true
}
}
return false
}

View File

@@ -1,110 +0,0 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"sort"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
)
// Status reports effective local/remote session state.
func Status(ctx context.Context, args []string, out io.Writer) error {
return statusWithDependencies(ctx, args, out, commandDependencies{})
}
func statusWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
fs := flag.NewFlagSet("status", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
addCommonConfigFlags(fs, &flags)
if err := parseSessionAwareFlags("status", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("status: session_id is required")
}
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return fmt.Errorf("status: %w", err)
}
defer func() { _ = loaded.Close() }()
cfg := loaded.Config
report, err := inspectSessionState(ctx, cfg, true, true, deps)
if report != nil {
err = errors.Join(err, commandkit.RenderReport(out, commandkit.Report{Value: report}, ""))
}
return err
}
func writeStatusStableInputs(out io.Writer, checks []stableInputCheck) {
if len(checks) == 0 {
return
}
for _, check := range checks {
if check.Err != nil {
if strings.TrimSpace(check.Path) != "" {
fmt.Fprintf(out, "Stable input %s: unavailable: %v\n", check.Name, check.Err)
} else {
fmt.Fprintf(out, "Stable input %s: unavailable: %s\n", check.Name, check.Err.Error())
}
continue
}
fmt.Fprintf(out, "Stable input %s: %s\n", check.Name, check.Path)
}
}
func writeStatusLocalAudio(out io.Writer, check localAudioCheck) {
if !check.Checked {
return
}
if check.Err != nil {
fmt.Fprintf(out, "Local audio: unavailable: %v\n", check.Err)
return
}
fmt.Fprintf(out, "Local audio: %d file(s)\n", len(check.Paths))
}
func writeStatusRemoteAudio(ctx context.Context, out io.Writer, cfg *config.Config, store storage.ObjectStore, storeErr error) {
if cfg.Session.Inputs.AudioS3 == nil {
return
}
if storeErr != nil {
fmt.Fprintf(out, "Remote audio: unavailable: %v\n", storeErr)
return
}
check := inspectRemoteAudioPresence(ctx, cfg, store)
if check.Err != nil {
fmt.Fprintf(out, "Remote audio: unavailable: %v\n", check.Err)
return
}
fmt.Fprintf(out, "Remote audio: %d .flac object(s)\n", len(check.Keys))
}
func writeStatusPreviousArtifacts(out io.Writer, readiness previousArtifactReadiness) {
if len(readiness.Requirements) == 0 {
fmt.Fprintln(out, "Previous-session artifacts: not required")
return
}
if readiness.Err != nil {
fmt.Fprintf(out, "Previous-session artifacts: unavailable: %v\n", readiness.Err)
return
}
names := make([]string, 0, len(readiness.Requirements))
for _, req := range readiness.Requirements {
names = append(names, fmt.Sprintf("%s(required=%t)", req.Name, req.Required))
}
sort.Strings(names)
if len(readiness.SkippedMissing) > 0 {
fmt.Fprintf(out, "Previous-session artifacts: ready: %s; optional unavailable: %s\n", strings.Join(names, ", "), strings.Join(readiness.SkippedMissing, ", "))
return
}
fmt.Fprintf(out, "Previous-session artifacts: ready: %s\n", strings.Join(names, ", "))
}

View File

@@ -22,23 +22,17 @@ func TestRunOverlaySelectionFlowsThroughSharedLoader(t *testing.T) {
return &RunSummary{SessionID: cfg.Session.SessionID, ManifestPath: "manifest.json"}, nil
}
if err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--overlay", "testing",
}, io.Discard, executor); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--overlay", "testing"}, io.Discard, executor, commandDependencies{}); err != nil {
t.Fatal(err)
}
if language != "fr" {
t.Fatalf("explicit overlay language = %q, want fr", language)
}
if err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--profile", "testing",
}, io.Discard, executor); err == nil {
if err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--profile", "testing"}, io.Discard, executor, commandDependencies{}); err == nil {
t.Fatal("retired pipeline selector was accepted")
}
if err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath,
}, io.Discard, executor); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, io.Discard, executor, commandDependencies{}); err != nil {
t.Fatal(err)
}
if language != "en" {
@@ -46,17 +40,13 @@ func TestRunOverlaySelectionFlowsThroughSharedLoader(t *testing.T) {
}
for _, overlay := range []string{"unknown"} {
err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--overlay", overlay,
}, io.Discard, executor)
err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--overlay", overlay}, io.Discard, executor, commandDependencies{})
if err == nil || !strings.Contains(err.Error(), "overlay") {
t.Fatalf("overlay %q error = %v, want selection rejection", overlay, err)
}
}
if err := runWithPipelineExecutor(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--overlay", "",
}, io.Discard, executor); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--overlay", ""}, io.Discard, executor, commandDependencies{}); err != nil {
t.Fatalf("explicit empty overlay should select base: %v", err)
}
}

View File

@@ -1,83 +1,14 @@
package app
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"strings"
"time"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/stage"
"go.maximumdirect.net/commandkit"
)
// Plan validates configuration and prints a read-only execution preview.
func Plan(ctx context.Context, args []string, out io.Writer) error {
return planWithDependencies(ctx, args, out, commandDependencies{})
}
func planWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
request, err := parseBoundedRunRequest("plan", args, out)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
flags := request.Config
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return fmt.Errorf("plan: %w", err)
}
defer func() { _ = loaded.Close() }()
cfg := loaded.Config
if err := config.Validate(cfg); err != nil {
return fmt.Errorf("plan: %w", err)
}
selectedArtifacts, err := normalizeArtifactSelection(cfg, request.SelectedArtifacts)
if err != nil {
return fmt.Errorf("plan: %w", err)
}
effective, err := resolveEffectiveArtifacts(cfg, selectedArtifacts)
if err != nil {
return fmt.Errorf("plan: %w", err)
}
input := &executionInput{plan: request.Plan, Force: commandkit.Supplied[bool]{Value: request.Force}}
report, err := buildSessionPlan(ctx, cfg, input, selectedArtifacts, effective)
if err != nil {
return err
}
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
}
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", entry.Name, entry.Action); err != nil {
return err
}
if entry.Analysis != nil {
if err := writeAnalyzePlanDetails(out, entry.Analysis); err != nil {
return err
}
}
}
_, 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) {
if source == nil {
created := manifest.New(cfg.Session.SessionID, time.Now().UTC())
@@ -125,49 +56,3 @@ func plannedSelfSkipReason(stageName string, cfg *config.Config) string {
}
return ""
}
func writeAnalyzePlanDetails(out io.Writer, summary *plannedAnalysis) error {
if summary == nil {
return 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.Prerequisites)); err != nil {
return err
}
if _, err := fmt.Fprintf(out, " execute: %s\n", planArtifactList(summary.Execute)); err != nil {
return err
}
_, err := fmt.Fprintf(out, " reuse: %s\n", planArtifactList(summary.Reuse))
return err
}
func planStringList(values []string) string {
if len(values) == 0 {
return "none"
}
return strings.Join(values, ", ")
}
func planArtifactList(values []plannedArtifact) string {
if len(values) == 0 {
return "none"
}
parts := make([]string, 0, len(values))
for _, value := range values {
detail := value.Role
if value.Reason != "" {
detail += ":" + value.Reason
}
if value.Forced {
detail += ":forced"
}
identity := value.Key
if value.Family != "" {
identity += fmt.Sprintf("[family=%s character_id=%s]", value.Family, value.CharacterID)
}
parts = append(parts, fmt.Sprintf("%s(%s)", identity, detail))
}
return strings.Join(parts, ", ")
}

View File

@@ -7,7 +7,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@@ -23,23 +23,17 @@ func TestPlanDoesNotCreateWorkdir(t *testing.T) {
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var out bytes.Buffer
args := []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}
args := []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}
if err := Plan(context.Background(), args, &out); err != nil {
if err := invokeNativeCommand(context.Background(), append([]string{"session", "plan"}, args...), &out); err != nil {
t.Fatalf("first Plan() error = %v", err)
}
got := out.String()
if !strings.Contains(got, "narratio session plan: read-only workdir at") {
t.Fatalf("first output = %q, want read-only workdir", got)
}
for _, name := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "extract", "analyze", "publish", "notify", "cleanup"} {
if !strings.Contains(got, name+": run") {
t.Fatalf("first output = %q, missing stage %q", got, name)
report := decodeNativeReport[sessionPlanReport](t, out.Bytes())
for index, name := range canonicalStageNames() {
if index >= len(report.Stages) || report.Stages[index].Name != name || report.Stages[index].Action != "run" {
t.Fatalf("initial plan=%#v", report)
}
}
if !strings.Contains(got, "totals: run=12 skip=0") {
t.Fatalf("first output = %q, want totals", got)
}
sessionWorkdir := artifacts.SessionWorkDirForCampaign(workspaceRoot, "sample-campaign", "2026-05-03")
if _, err := os.Stat(sessionWorkdir); !errors.Is(err, os.ErrNotExist) {
@@ -47,12 +41,13 @@ func TestPlanDoesNotCreateWorkdir(t *testing.T) {
}
out.Reset()
if err := Plan(context.Background(), args, &out); err != nil {
if err := invokeNativeCommand(context.Background(), append([]string{"session", "plan"}, args...), &out); err != nil {
t.Fatalf("second Plan() error = %v", err)
}
if !strings.Contains(out.String(), "narratio session plan: read-only workdir at") {
t.Fatalf("second output = %q, want read-only workdir", out.String())
if repeated := decodeNativeReport[sessionPlanReport](t, out.Bytes()); !reflect.DeepEqual(repeated, report) {
t.Fatalf("read-only plan changed: %#v", repeated)
}
}
func TestPlanShowsRunAndSkipFromManifest(t *testing.T) {
@@ -73,19 +68,23 @@ func TestPlanShowsRunAndSkipFromManifest(t *testing.T) {
}
var out bytes.Buffer
if err := Plan(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out); err != nil {
if err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out); err != nil {
t.Fatalf("Plan() error = %v", err)
}
got := out.String()
if !strings.Contains(got, "prepare: skip") || !strings.Contains(got, "transcribe: skip") {
t.Fatalf("output = %q, want prepare/transcribe skipped", got)
report := decodeNativeReport[sessionPlanReport](t, out.Bytes())
skipped := 0
for _, item := range report.Stages {
if item.Action == "skip" {
skipped++
if item.Name != "prepare" && item.Name != "transcribe" {
t.Fatalf("unexpected reuse: %#v", item)
}
}
}
if !strings.Contains(got, "trim: run") {
t.Fatalf("output = %q, want trim run", got)
}
if !strings.Contains(got, "totals: run=10 skip=2") {
t.Fatalf("output = %q, want totals run=10 skip=2", got)
if skipped != 2 || len(report.Stages) != len(canonicalStageNames()) {
t.Fatalf("plan=%#v", report)
}
}
func TestPlanSkipsAbsentConventionalSecretsDirectory(t *testing.T) {
@@ -124,7 +123,7 @@ inputs:
}
var out bytes.Buffer
err := Plan(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("Plan() error = %v, want absent conventional secrets directory skipped", err)
}
@@ -179,24 +178,31 @@ func TestPlanAndRunShareAnalyzeArtifactDecisionsWithoutPlanSideEffects(t *testin
t.Setenv("PATH", binaryDir+string(os.PathListSeparator)+os.Getenv("PATH"))
var out bytes.Buffer
err = Plan(context.Background(), []string{
"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath,
err = invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath,
"--from", "render", "--through", "analyze",
}, &out)
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
got := out.String()
for _, want := range []string{
"render: run", "extract: run", "analyze: run",
" targets: player_handout, session_recap",
" prerequisites: none", " execute: none",
"player_handout(target:current)", "session_recap(target:current)",
} {
if !strings.Contains(got, want) {
t.Fatalf("plan output = %q, want %q", got, want)
report := decodeNativeReport[sessionPlanReport](t, out.Bytes())
if len(report.Stages) != 3 {
t.Fatalf("bounded plan=%#v", report)
}
for _, item := range report.Stages {
if item.Action != "run" {
t.Fatalf("stage plan=%#v", item)
}
}
analysis := report.Stages[2].Analysis
if analysis == nil || len(analysis.Execute) != 0 || len(analysis.Prerequisites) != 0 || len(analysis.Reuse) != 2 {
t.Fatalf("analysis plan=%#v", analysis)
}
for _, item := range analysis.Reuse {
if item.Role != "target" || item.Reason != "current" {
t.Fatalf("artifact reuse=%#v", item)
}
}
manifestAfter, err := os.ReadFile(first.ManifestPath)
if err != nil {
t.Fatal(err)

View File

@@ -140,22 +140,3 @@ func BuildSingleStagePlan(name string) ([]stage.Stage, error) {
}
return []stage.Stage{s}, nil
}
// buildSingleStageExecutionPlan selects one canonical stage without applying
// bounded-run prefix prerequisites. The run-stage family validates the stage's
// concrete inputs and intentionally retains its established direct-execution
// semantics.
func buildSingleStageExecutionPlan(name string) (BoundedPlan, error) {
stages, err := BuildSingleStagePlan(name)
if err != nil {
return BoundedPlan{}, err
}
plan, err := BuildBoundedPlan(name, name)
if err != nil {
return BoundedPlan{}, fmt.Errorf("build stage plan: %w", err)
}
plan.stages = stages
plan.explicitFrom = false
plan.explicitThrough = false
return plan, nil
}

View File

@@ -76,22 +76,23 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
}
setSessionConfigSearchPaths(t, []string{sessionPath})
deps, _ := remoteSessionLoaderDependencies(fake)
loaded, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps)
loaded := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), Session: suppliedSelection(m.SessionID)}, deps)
received, err := loaded.Domain(ctx)
if err != nil {
t.Fatal(err)
}
defer func() { _ = loaded.Close() }()
if loaded.Config.Session.Title != "Published title" || loaded.Config.SessionSource.S3Key != key {
t.Fatalf("remote session = %#v, source=%#v", loaded.Config.Session, loaded.Config.SessionSource)
if received.Session.Title != "Published title" || received.SessionSource.S3Key != key {
t.Fatalf("remote session = %#v, source=%#v", received.Session, received.SessionSource)
}
if got := loaded.Config.StableInputs.SpeakersFile; got.Source != "session_config" || got.Path != filepath.Join(filepath.Dir(campaignPath), "speakers.yml") {
if got := received.StableInputs.SpeakersFile; got.Source != "session_config" || got.Path != filepath.Join(filepath.Dir(campaignPath), "speakers.yml") {
t.Fatalf("receiving campaign speakers = %#v", got)
}
current, err := discoverRemoteCurrentState(ctx, loaded.Config, fake)
current, err := discoverRemoteCurrentState(ctx, received, fake)
if err != nil {
t.Fatal(err)
}
plan, err := buildRestorePlan(ctx, loaded.Config, current, fake, RestorePlanOptions{Force: true})
plan, err := buildRestorePlan(ctx, received, current, fake, RestorePlanOptions{Force: true})
if err != nil {
t.Fatal(err)
}
@@ -113,7 +114,7 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
}
}
var output strings.Builder
if err := restoreWithDependencies(ctx, []string{m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--force"}, &output, deps); err != nil {
if err := invokeNativeCommandWith(ctx, []string{"session", "restore", m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--force"}, &output, executePlan, deps); err != nil {
t.Fatalf("restore: %v", err)
}
restored, err := os.ReadFile(filepath.Join(paths.InputsDir, "session.yml"))
@@ -121,21 +122,22 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
t.Fatalf("restored session = %q, %v", restored, err)
}
committedBytes := string(fake.Objects[key].Data)
if err := sessionInitWithDependencies(ctx, []string{m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--remote", "--force", "--title", "Updated title", "--audio-s3-prefix", "replacement/"}, &output, deps); err != nil {
if err := invokeNativeCommandWith(ctx, []string{"session", "init", m.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--remote", "--force", "--title", "Updated title", "--audio-s3-prefix", "replacement/"}, &output, executePlan, deps); err != nil {
t.Fatal(err)
}
updated, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps)
updated := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), Session: suppliedSelection(m.SessionID)}, deps)
authored, err := updated.Domain(ctx)
if err != nil {
t.Fatal(err)
}
defer func() { _ = updated.Close() }()
if updated.Config.Session.Title != "Updated title" || updated.Config.Session.Inputs.AudioS3 == nil || updated.Config.Session.Inputs.AudioS3.Prefix != "replacement/" {
t.Fatalf("remote edit ignored: %#v", updated.Config.Session)
if authored.Session.Title != "Updated title" || authored.Session.Inputs.AudioS3 == nil || authored.Session.Inputs.AudioS3.Prefix != "replacement/" {
t.Fatalf("remote edit ignored: %#v", authored.Session)
}
if string(fake.Objects[key].Data) != committedBytes {
t.Fatal("authoring update changed committed input")
}
if _, err := discoverRemoteCurrentState(ctx, updated.Config, fake); err != nil {
if _, err := discoverRemoteCurrentState(ctx, authored, fake); err != nil {
t.Fatalf("committed state after authoring update: %v", err)
}
// Remove the authored override to exercise committed-snapshot integrity below.
@@ -147,7 +149,9 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
modified := append([]byte(nil), object.Data...)
modified[0] ^= 1
fake.SeedObject(storage.FakeObject{Key: key, Data: modified, ETag: object.ETag})
if _, err := loadCommandConfigWithDependencies(ctx, pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: m.SessionID}, nil, deps); err == nil || !strings.Contains(err.Error(), "committed checksum") {
tampered := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), Session: suppliedSelection(m.SessionID)}, deps)
defer func() { _ = tampered.Close() }()
if _, err := tampered.Domain(ctx); err == nil || !strings.Contains(err.Error(), "committed checksum") {
t.Fatalf("changed remote session error = %v", err)
}
}

View File

@@ -104,10 +104,11 @@ inputs:
mustWriteTestFile(t, basePath, `{"schema":"seriatim-intermediate","segments":[]}`)
seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "merge", artifacts.TranscriptOutputKindBase, artifacts.ArtifactTranscriptBase, basePath)
mustWriteTestFile(t, filepath.Join(workRoot, "inputs", "glossary.yml"), "[]\n")
args := []string{"--secrets-dir", secretsDir, "--config", configPath, "--campaign-file", campaignPath, "--session", sessionPath}
seedInvocationPrerequisites(t, workspace, "polish")
args := []string{"--secrets-dir", secretsDir, "--config", configPath, "--campaign-file", campaignPath, "--session-file", sessionPath}
stdout.Reset()
stderr.Reset()
if code := executeForTest(append([]string{"run-stage", "polish", "2026-05-03", "--force"}, args...), &stdout, &stderr); code != 0 {
if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "polish", "--force"}, args...), &stdout, &stderr); code != 0 {
t.Fatalf("polish exit = %d; stderr=%q", code, stderr.String())
}
assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes())
@@ -136,17 +137,18 @@ inputs:
}
stdout.Reset()
stderr.Reset()
if code := executeForTest(append([]string{"run-stage", "extract", "2026-05-03", "--force"}, args...), &stdout, &stderr); code != 0 {
seedInvocationPrerequisites(t, workspace, "extract")
if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "extract", "--force"}, args...), &stdout, &stderr); code != 0 {
t.Fatalf("extract exit = %d; stderr=%q", code, stderr.String())
}
assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes())
if !strings.Contains(stdout.String(), "stage=extract executed=1") {
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "extract") {
t.Fatalf("extract did not execute: %q", stdout.String())
}
redactedLogs := 0
stdout.Reset()
stderr.Reset()
if code := executeForTest(append([]string{"analyze", "2026-05-03"}, args...), &stdout, &stderr); code != 0 {
if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "analyze", "--force"}, args...), &stdout, &stderr); code != 0 {
t.Fatalf("analyze exit = %d; stderr=%q", code, stderr.String())
}
assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes())

View File

@@ -38,7 +38,7 @@ inputs:
if storeInitCalls != 1 {
t.Fatalf("object store init calls = %d, want 1", storeInitCalls)
}
if !strings.Contains(stdout.String(), "narratio session plan: read-only workdir") {
if decodeNativeReport[sessionPlanReport](t, stdout.Bytes()).SessionID != "2026-05-03" {
t.Fatalf("stdout = %q, want plan output", stdout.String())
}
if _, ok := fake.Objects[remoteKey]; !ok {
@@ -97,13 +97,14 @@ inputs:
setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "session.yml")})
deps, storeInitCalls := remoteSessionLoaderDependencies(fake)
loaded, err := loadCommandConfigWithDependencies(context.Background(), pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: "2026-05-03"}, nil, deps)
loaded := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), Session: suppliedSelection("2026-05-03")}, deps)
resolved, err := loaded.Domain(context.Background())
if err != nil {
t.Fatalf("loadCommandConfig() error = %v", err)
}
defer func() { _ = loaded.Close() }()
if loaded.Config.Party.Canonical == nil {
t.Fatalf("remote config party = %#v", loaded.Config.Party)
if resolved.Party.Canonical == nil {
t.Fatalf("remote config party = %#v", resolved.Party)
}
if *storeInitCalls != 1 {
t.Fatalf("object store init calls = %d, want 1", *storeInitCalls)
@@ -218,18 +219,19 @@ inputs:
setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "session.yml")})
deps, _ := remoteSessionLoaderDependencies(fake)
loaded, err := loadCommandConfigWithDependencies(context.Background(), pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: "2026-05-03"}, nil, deps)
loaded := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipelinePath), CampaignFile: suppliedSelection(campaignPath), Session: suppliedSelection("2026-05-03")}, deps)
resolved, err := loaded.Domain(context.Background())
if err != nil {
t.Fatalf("loadCommandConfig() error = %v", err)
}
downloadedPath := loaded.Config.SessionSource.LocalPath
downloadedPath := resolved.SessionSource.LocalPath
if err := loaded.Close(); err != nil {
t.Fatalf("first Close() error = %v", err)
}
if err := loaded.Close(); err != nil {
t.Fatalf("second Close() error = %v", err)
}
if loaded.Config == nil || loaded.Config.Pipeline == nil || loaded.Config.Campaign == nil || loaded.Config.Session == nil {
if resolved == nil || resolved.Pipeline == nil || resolved.Campaign == nil || resolved.Session == nil {
t.Fatal("closing remote session cleanup discarded retained configuration")
}
if _, err := os.Stat(downloadedPath); !errors.Is(err, os.ErrNotExist) {
@@ -289,7 +291,7 @@ func TestExecuteExplicitLocalSessionPrecedenceSkipsRemote(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr, deps, executePlan)
code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
@@ -335,9 +337,7 @@ func TestExecuteRemoteSessionMissingObjectFailsClearly(t *testing.T) {
if !strings.Contains(stderr.String(), "remote session") || !strings.Contains(stderr.String(), "session.yml") || !strings.Contains(stderr.String(), "not found") {
t.Fatalf("stderr = %q, want remote session not found context", stderr.String())
}
if !strings.Contains(stderr.String(), missingSessionPath) {
t.Fatalf("stderr = %q, want local searched path", stderr.String())
}
}
func TestExecuteRemoteSessionRequiresSessionID(t *testing.T) {
@@ -353,7 +353,7 @@ func TestExecuteRemoteSessionRequiresSessionID(t *testing.T) {
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "plan: session_id is required") {
if code != 2 {
t.Fatalf("stderr = %q, want session_id guidance", stderr.String())
}
if storeInitCalls != 0 {

View File

@@ -1,63 +0,0 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"strings"
"go.maximumdirect.net/commandkit"
)
// Restore loads the selected session and restores its committed remote snapshot.
func Restore(ctx context.Context, args []string, out io.Writer) (resultErr error) {
return restoreWithDependencies(ctx, args, out, commandDependencies{})
}
func restoreWithDependencies(ctx context.Context, args []string, out io.Writer, deps commandDependencies) (resultErr error) {
deps = normalizeCommandDependencies(deps)
positionalSessionID, args := pullLeadingSessionID(args)
fs := flag.NewFlagSet("restore", flag.ContinueOnError)
fs.SetOutput(out)
var flags commonConfigFlags
var dryRun bool
var force bool
var includeAudio bool
addCommonConfigFlags(fs, &flags)
fs.BoolVar(&dryRun, "dry-run", false, "plan restore actions without writing local files")
fs.BoolVar(&force, "force", false, "overwrite local conflicts with remote state")
fs.BoolVar(&includeAudio, "include-audio", false, "include remote session-level audio objects")
fs.Usage = func() {
_, _ = fmt.Fprintln(out, "Usage: narratio session restore <session_id> [--config <path>] [--campaign <id>] [--campaign-file <path>] [--session <path>] [--previous-session-id <value>] [--dry-run] [--force] [--include-audio]")
_, _ = fmt.Fprintln(out)
_, _ = fmt.Fprintln(out, "Flags:")
fs.PrintDefaults()
}
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return fmt.Errorf("restore: invalid flags: %w", err)
}
if err := resolveParsedSessionID("restore", positionalSessionID, fs, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("restore: session_id is required")
}
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return fmt.Errorf("restore: %w", err)
}
defer func() { _ = loaded.Close() }()
cfg := loaded.Config
report, err := restoreSession(ctx, cfg, RestorePlanOptions{DryRun: dryRun, Force: force, IncludeAudio: includeAudio}, deps)
if report != nil {
err = errors.Join(err, commandkit.RenderReport(out, commandkit.Report{Value: report}, ""))
}
return err
}

View File

@@ -51,7 +51,7 @@ func restoreSessionState(ctx context.Context, cfg *config.Config, opts RestorePl
if err := config.Validate(cfg); err != nil {
return diagnostic, fmt.Errorf("restore: %w", err)
}
objectStore, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
objectStore, err := newCommandObjectStoreWithDependencies(ctx, cfg, deps)
if err != nil {
return diagnostic, fmt.Errorf("restore: %w", err)
}

View File

@@ -1,7 +1,6 @@
package app
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
@@ -150,11 +149,12 @@ func TestCommittedStatusReportsOnlyDeclaredPublishedOutputs(t *testing.T) {
current.Commit.Artifacts[1].Source = "narratio.transcript.final_trimmed"
fake.SeedObject(storage.FakeObject{Key: current.SessionPrefix + "transcripts/full.json", Data: []byte("mutable stale copy\n")})
availability := remotePublishedOutputAvailabilityForCurrent(context.Background(), cfg, fake, nil, current)
key := publishedOutputRemoteStateKey("narratio.transcript.final_trimmed", "transcripts/full.json")
if availability[key] != "remote=published" {
t.Fatalf("availability = %#v, want committed published output", availability)
cfg.ArtifactCatalog = &config.ArtifactCatalog{PublishOutputs: cfg.Pipeline.Publish.Outputs}
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, nil, true, "available")
if err != nil || len(outputs) != 1 || outputs[0].State != "published" {
t.Fatalf("outputs=%#v err=%v", outputs, err)
}
}
func TestCommittedStatusReportsCarriedOutputOriginAndChecksExactKey(t *testing.T) {
@@ -180,22 +180,18 @@ func TestCommittedStatusReportsCarriedOutputOriginAndChecksExactKey(t *testing.T
}}},
}
availability := remotePublishedOutputAvailabilityForCurrent(context.Background(), cfg, fake, nil, current)
key := publishedOutputRemoteStateKey("narratio.transcript.final_trimmed", "transcripts/full.json")
want := "outcome=carried_locked remote=published declared_dest=transcripts/original.json declared_key=" + oldKey + " origin_run=" + originRun
if availability[key] != want {
t.Fatalf("availability = %#v, want %q", availability, want)
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, nil, true, "available")
if err != nil || len(outputs) != 1 {
t.Fatalf("outputs=%#v err=%v", outputs, err)
}
output := outputs[0]
if output.Outcome != "carried_locked" || output.State != "published" || output.Destination != "transcripts/full.json" || output.DeclaredDestination != "transcripts/original.json" || output.DeclaredKey != oldKey || output.OriginRun != originRun {
t.Fatalf("carried output=%#v", output)
}
if len(fake.ExistsKeys) == 0 || fake.ExistsKeys[len(fake.ExistsKeys)-1] != oldKey {
t.Fatalf("exists keys = %#v, want final exact declaration key %q", fake.ExistsKeys, oldKey)
}
var rendered bytes.Buffer
writePublishedOutputLine(&rendered, cfg.ArtifactCatalog.PublishOutputs[0], nil, nil, availability)
for _, fragment := range []string{"dest=transcripts/full.json", "declared_dest=transcripts/original.json", "declared_key=" + oldKey, "origin_run=" + originRun} {
if !strings.Contains(rendered.String(), fragment) {
t.Fatalf("rendered output = %q, want %q", rendered.String(), fragment)
}
t.Fatalf("exists keys=%v, want exact carried key", fake.ExistsKeys)
}
}
func TestCommittedPublishOutputOutcomesRecognizesAllStates(t *testing.T) {

View File

@@ -393,7 +393,7 @@ func TestExecuteRestoreLockConflictFailsAndWritesNothing(t *testing.T) {
cancel()
return current, err
}
err = restoreWithDependencies(ctx, []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &bytes.Buffer{}, deps)
err = invokeNativeCommandWith(ctx, []string{"session", "restore", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &bytes.Buffer{}, executePlan, deps)
if err == nil || !strings.Contains(err.Error(), "acquire session lock") {
t.Fatalf("Restore() error = %v, want lock failure", err)
}

View File

@@ -494,46 +494,3 @@ func restoreRelativePathIsAudio(rel string) bool {
cleanRel := path.Clean(strings.TrimSpace(rel))
return cleanRel == config.PathAudioDirSegment || strings.HasPrefix(cleanRel, config.PathAudioDirSegment+"/")
}
func writeRestorePlan(out io.Writer, current *RemoteCurrentState, plan *RestorePlan, opts RestorePlanOptions) error {
if out == nil {
return fmt.Errorf("output writer is required")
}
if current == nil {
return fmt.Errorf("remote current state is required")
}
if plan == nil {
return fmt.Errorf("restore plan is required")
}
if _, err := fmt.Fprintf(
out,
"restore plan: session %s/%s run=%s actions=%d download=%d skip_same=%d conflict=%d dry_run=%t force=%t include_audio=%t\n",
current.Campaign,
current.SessionID,
current.RunID,
len(plan.Actions),
plan.DownloadCount,
plan.SkipSameCount,
plan.ConflictCount,
opts.DryRun,
opts.Force,
opts.IncludeAudio,
); err != nil {
return err
}
for _, action := range plan.Actions {
if _, err := fmt.Fprintf(out, "%s %s <- %s", action.Kind, action.LocalRelativePath, action.RemoteKey); err != nil {
return err
}
if strings.TrimSpace(action.Reason) != "" {
if _, err := fmt.Fprintf(out, " (%s)", action.Reason); err != nil {
return err
}
}
if _, err := fmt.Fprintln(out); err != nil {
return err
}
}
return nil
}

View File

@@ -502,19 +502,3 @@ func restorePlanCurrentState(t *testing.T, cfg *config.Config) *RemoteCurrentSta
},
}
}
func TestWriteRestorePlan(t *testing.T) {
current := &RemoteCurrentState{Campaign: "sample-campaign", SessionID: "2026-05-03", RunID: "r-1"}
plan := &RestorePlan{Actions: []RestoreAction{{Kind: RestoreActionDownload, LocalRelativePath: "manifest.json", RemoteKey: "k", Reason: "local file missing"}}, DownloadCount: 1}
var out strings.Builder
if err := writeRestorePlan(&out, current, plan, RestorePlanOptions{DryRun: true}); err != nil {
t.Fatalf("writeRestorePlan() error = %v", err)
}
text := out.String()
if !strings.Contains(text, "restore plan: session sample-campaign/2026-05-03 run=r-1") {
t.Fatalf("output = %q, want plan summary", text)
}
if !strings.Contains(text, "download manifest.json <- k") {
t.Fatalf("output = %q, want action line", text)
}
}

View File

@@ -39,12 +39,10 @@ func TestRestoreHoldsTransitionLockBeforePlanning(t *testing.T) {
restoreDone := make(chan error, 1)
go func() {
restoreDone <- restoreWithDependencies(context.Background(), []string{
cfg.Session.SessionID,
restoreDone <- invokeNativeCommandWith(context.Background(), []string{"session", "restore", cfg.Session.SessionID,
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
}, &bytes.Buffer{}, deps)
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &bytes.Buffer{}, executePlan, deps)
}()
<-planning

View File

@@ -3,9 +3,8 @@ package app
import (
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
@@ -154,75 +153,6 @@ func (r *RestoreReport) findAction(action RestoreAction) int {
return -1
}
func writeRestoreDryRunSummary(out io.Writer, report *RestoreReport) error {
if out == nil {
return fmt.Errorf("output writer is required")
}
if report == nil {
return fmt.Errorf("restore report is required")
}
if _, err := fmt.Fprintf(out, "Restore plan for %s/%s\n", report.Campaign, report.SessionID); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Remote run: %s\n", report.RunID); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Would download: %d\n", report.Plan.Download); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Would skip unchanged: %d\n", report.Plan.SkipSame); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Conflicts: %d\n", report.Plan.Conflicts); err != nil {
return err
}
if _, err := fmt.Fprintln(out, "Remote current-state and object-identity checks may read remote data; no session files will be written."); err != nil {
return err
}
for _, action := range report.Actions {
line := ""
switch action.Status {
case "planned_download":
line = "Would download: " + action.LocalRelativePath
case "skipped_same":
line = "Would skip unchanged: " + action.LocalRelativePath
case "conflict":
line = "Conflict: " + action.LocalRelativePath
default:
line = strings.TrimSpace(action.Kind) + ": " + action.LocalRelativePath
}
if _, err := fmt.Fprintln(out, line); err != nil {
return err
}
}
return nil
}
func writeRestoreSuccessSummary(out io.Writer, report *RestoreReport) error {
if out == nil {
return fmt.Errorf("output writer is required")
}
if report == nil {
return fmt.Errorf("restore report is required")
}
if _, err := fmt.Fprintf(out, "Restored session state for %s/%s\n", report.Campaign, report.SessionID); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Remote run: %s\n", report.RunID); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Downloaded: %d\n", report.Execution.Downloaded); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Skipped unchanged: %d\n", report.Plan.SkipSame); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Conflicts: %d\n", report.Plan.Conflicts); err != nil {
return err
}
return nil
}
func persistRestoreReport(store artifacts.Store, cfg *config.Config, report *RestoreReport) (string, error) {
if store == nil {
return "", fmt.Errorf("artifact store is required")

View File

@@ -120,7 +120,7 @@ func TestExecuteRestoreFailsWhenStorageBackendNotConfigured(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"session", "restore", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr)
code := executeWithDependencies([]string{"session", "restore", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr, commandDependencies{newObjectStore: storage.NewObjectStoreFromConfig}, executePlan)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}

View File

@@ -16,7 +16,7 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
func TestRestoreThenForcedAnalyzeUsesRestoredDurableState(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
@@ -43,8 +43,7 @@ func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
[]string{
"session", "restore", cfg.Session.SessionID,
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
},
&stdout,
&stderr,
@@ -67,7 +66,7 @@ func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
t.Fatalf("load restored session manifest: %v", err)
}
upstreamCompletedAt := map[string]time.Time{}
for _, stageName := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim"} {
for _, stageName := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "extract"} {
rec := beforeAnalyze.Stages[stageName]
if rec == nil || rec.Status != manifest.StatusSucceeded || rec.CompletedAt == nil {
t.Fatalf("restored manifest stage %q = %#v, want succeeded with completion timestamp", stageName, rec)
@@ -77,12 +76,10 @@ func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
stdout.Reset()
stderr.Reset()
runStageCode := executeWithPipelineExecutor(
[]string{
"run-stage", "analyze", cfg.Session.SessionID,
runCode := executeWithPipelineExecutor(
[]string{"run", cfg.Session.SessionID, "--stage", "analyze",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--force",
"--artifacts", "player_handout",
},
@@ -90,14 +87,14 @@ func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
&stderr,
executor,
)
if runStageCode != 0 {
t.Fatalf("run-stage exit code = %d, want 0; stderr=%q", runStageCode, stderr.String())
if runCode != 0 {
t.Fatalf("single-stage run exit code = %d, want 0; stderr=%q", runCode, stderr.String())
}
if stderr.Len() != 0 {
t.Fatalf("run-stage stderr = %q, want empty", stderr.String())
t.Fatalf("single-stage run stderr = %q, want empty", stderr.String())
}
if !strings.Contains(stdout.String(), "stage=analyze executed=1 skipped=0 force=true") {
t.Fatalf("run-stage stdout = %q, want analyze execution summary", stdout.String())
if !runCountsMatch(t, stdout.Bytes(), 1, 0, "analyze") {
t.Fatalf("single-stage run stdout = %q, want analyze execution summary", stdout.String())
}
playerHandoutPath := filepath.Join(sessionRoot, "artifacts", "player_handout.md")
@@ -107,9 +104,9 @@ func TestRestoreThenRunStageForceAnalyzeUsesRestoredDurableState(t *testing.T) {
afterAnalyze, err := manifestStore.Load(context.Background(), sessionManifestPath)
if err != nil {
t.Fatalf("load session manifest after run-stage analyze: %v", err)
t.Fatalf("load session manifest after single-stage analysis: %v", err)
}
for _, stageName := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim"} {
for _, stageName := range []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "extract"} {
rec := afterAnalyze.Stages[stageName]
if rec == nil || rec.Status != manifest.StatusSucceeded || rec.CompletedAt == nil {
t.Fatalf("post-analyze manifest stage %q = %#v, want succeeded with completion timestamp", stageName, rec)
@@ -172,8 +169,7 @@ previous_session_id: 2026-04-26
[]string{
"session", "restore", cfg.Session.SessionID,
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
},
&stdout,
&stderr,
@@ -222,12 +218,10 @@ previous_session_id: 2026-04-26
stdout.Reset()
stderr.Reset()
runStageCode := executeWithPipelineExecutor(
[]string{
"run-stage", "analyze", cfg.Session.SessionID,
runCode := executeWithPipelineExecutor(
[]string{"run", cfg.Session.SessionID, "--stage", "analyze",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--force",
"--artifacts", "session_recap",
},
@@ -235,14 +229,14 @@ previous_session_id: 2026-04-26
&stderr,
executor,
)
if runStageCode != 0 {
t.Fatalf("run-stage exit code = %d, want 0; stderr=%q", runStageCode, stderr.String())
if runCode != 0 {
t.Fatalf("single-stage run exit code = %d, want 0; stderr=%q", runCode, stderr.String())
}
if stderr.Len() != 0 {
t.Fatalf("run-stage stderr = %q, want empty", stderr.String())
t.Fatalf("single-stage run stderr = %q, want empty", stderr.String())
}
if objectStoreConstructed {
t.Fatal("analyze run-stage should not construct object store for previous-session input resolution")
t.Fatal("single-stage analysis should not construct object store for previous-session input resolution")
}
if len(scriptoriumFake.RunRequests) != 1 {
t.Fatalf("scriptorium run requests = %d, want 1", len(scriptoriumFake.RunRequests))
@@ -263,7 +257,7 @@ func restoreWorkflowManifestJSON(t *testing.T, sessionID, campaign string) []byt
m := manifest.New(sessionID, now)
m.Campaign = campaign
m.RunID = "20260519T010203Z-a1b2c3d4"
stages := []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim"}
stages := []string{"prepare", "transcribe", "merge", "polish", "normalize", "trim", "render", "extract"}
for i, stageName := range stages {
m.MarkStageSucceeded(stageName, now.Add(time.Duration(i+1)*time.Minute), nil)
}

View File

@@ -1,68 +0,0 @@
package app
import (
"context"
"errors"
"flag"
"fmt"
"io"
"gitea.maximumdirect.net/eric/narratio/internal/config"
)
// Run executes the pipeline plan and persists manifest state.
func Run(ctx context.Context, args []string, out io.Writer) error {
return runWithDependencies(ctx, args, out, executePlan, commandDependencies{})
}
func runWithPipelineExecutor(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor) error {
return runWithDependencies(ctx, args, out, executor, commandDependencies{})
}
func runWithDependencies(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
deps = normalizeCommandDependencies(deps)
request, err := parseBoundedRunRequest("run", args, out)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
flags := request.Config
loaded, err := loadCommandConfigWithDependencies(ctx, flags.pipelinePath, flags.campaignPath, flags.campaignFilePath, flags.sessionPath, flags.sessionOptions(), flags.secretsDir.pointer(), deps)
if err != nil {
return fmt.Errorf("run: %w", err)
}
defer func() { _ = loaded.Close() }()
cfg := loaded.Config
if err := config.Validate(cfg); err != nil {
return fmt.Errorf("run: %w", err)
}
selectedArtifacts, err := normalizeArtifactSelection(cfg, request.SelectedArtifacts)
if err != nil {
return fmt.Errorf("run: %w", err)
}
effectiveArtifacts, err := resolveEffectiveArtifacts(cfg, selectedArtifacts)
if err != nil {
return fmt.Errorf("run: %w", err)
}
summary, err := executor(ctx, cfg, request.Plan, RunOptions{
Force: request.Force,
SelectedArtifacts: selectedArtifacts,
EffectiveArtifacts: effectiveArtifacts,
})
if err != nil {
return fmt.Errorf("run: %w", err)
}
_, err = fmt.Fprintf(
out,
"narratio run: session %s; executed=%d skipped=%d; manifest=%s; %s\n",
summary.SessionID,
len(summary.Executed),
len(summary.Skipped),
summary.ManifestPath,
effectiveConfigSummary(cfg),
)
return err
}

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
@@ -34,11 +34,11 @@ func TestRunContinuesAfterCompletedStages(t *testing.T) {
mustWriteTestFile(t, filepath.Join(workRoot, "transcripts", "raw", "alice.json"), `{"segments":[]}`)
var out bytes.Buffer
err = Run(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err = invokeNativeCommand(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !strings.Contains(out.String(), "executed=10 skipped=4") {
if !runCountsMatch(t, out.Bytes(), 10, 4, "") {
t.Fatalf("output = %q, want executed=10 skipped=4", out.String())
}
@@ -72,11 +72,11 @@ func TestRunNoRemainingStagesRecordsSkippedStages(t *testing.T) {
}
var out bytes.Buffer
err = Run(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err = invokeNativeCommand(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !strings.Contains(out.String(), "executed=2 skipped=12") {
if !runCountsMatch(t, out.Bytes(), 2, 12, "") {
t.Fatalf("output = %q, want disabled extraction to self-skip", out.String())
}
loaded, err := store.Load(context.Background(), manifestPath)
@@ -108,16 +108,16 @@ func TestRunForceRerunsSucceeded(t *testing.T) {
}
var out bytes.Buffer
err := Run(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force"}, &out)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !strings.Contains(out.String(), "executed=12 skipped=2") {
if !runCountsMatch(t, out.Bytes(), 12, 2, "") {
t.Fatalf("output = %q, want forced full rerun", out.String())
}
}
func TestRunStageExecutesOnlySelectedStage(t *testing.T) {
func TestRunSingleStageExecutesOnlySelectedStage(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -127,12 +127,14 @@ func TestRunStageExecutesOnlySelectedStage(t *testing.T) {
seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "merge", artifacts.TranscriptOutputKindBase, artifacts.ArtifactTranscriptBase, basePath)
mustWriteTestFile(t, filepath.Join(workRoot, "inputs", "glossary.yml"), "terms: []\n")
seedInvocationPrerequisites(t, workspaceRoot, "polish")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage() error = %v", err)
t.Fatalf("single-stage run error = %v", err)
}
if !strings.Contains(out.String(), "stage=polish executed=1 skipped=0") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "polish") {
t.Fatalf("output = %q, want stage executed", out.String())
}
@@ -144,21 +146,23 @@ func TestRunStageExecutesOnlySelectedStage(t *testing.T) {
if m.Stages["polish"] == nil || m.Stages["polish"].Status != manifest.StatusSucceeded {
t.Fatalf("polish stage = %#v, want succeeded", m.Stages["polish"])
}
if m.Stages["prepare"] != nil {
t.Fatalf("prepare should not run in run-stage, got %#v", m.Stages["prepare"])
if m.Stages["prepare"] != nil && m.Stages["prepare"].StartedAt != nil {
t.Fatalf("prepare was attempted outside the selected range: %#v", m.Stages["prepare"])
}
}
func TestRunStageExtractIsAcceptedAndSelfSkipsWhenDisabled(t *testing.T) {
func TestRunSingleStageExtractIsAcceptedAndSelfSkipsWhenDisabled(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
seedInvocationPrerequisites(t, workspaceRoot, "extract")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"extract", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "extract", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage(extract) error = %v", err)
t.Fatalf("single-stage run (extract) error = %v", err)
}
if !strings.Contains(out.String(), "stage=extract executed=1 skipped=1 force=false") {
if !runCountsMatch(t, out.Bytes(), 1, 1, "extract") {
t.Fatalf("output = %q, want disabled extraction self-skip", out.String())
}
loaded, err := (&manifest.LocalStore{}).Load(context.Background(), filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json"))
@@ -170,7 +174,7 @@ func TestRunStageExtractIsAcceptedAndSelfSkipsWhenDisabled(t *testing.T) {
}
}
func TestRunStageSkipAndForce(t *testing.T) {
func TestRunSingleStageSkipAndForce(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -188,26 +192,28 @@ func TestRunStageSkipAndForce(t *testing.T) {
t.Fatalf("save manifest: %v", err)
}
seedInvocationPrerequisites(t, workspaceRoot, "polish")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage() error = %v", err)
t.Fatalf("single-stage run error = %v", err)
}
if !strings.Contains(out.String(), "stage=polish executed=0 skipped=1 force=false") {
if !runCountsMatch(t, out.Bytes(), 0, 1, "polish") {
t.Fatalf("output = %q, want skip", out.String())
}
out.Reset()
err = RunStage(context.Background(), []string{"polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"}, &out)
err = invokeNativeCommand(context.Background(), []string{"run", "--stage", "polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force"}, &out)
if err != nil {
t.Fatalf("RunStage(force) error = %v", err)
t.Fatalf("single-stage run (force) error = %v", err)
}
if !strings.Contains(out.String(), "stage=polish executed=1 skipped=0 force=true") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "polish") {
t.Fatalf("output = %q, want force rerun", out.String())
}
}
func TestRunStageForceMarksDownstreamStaleAndRunContinuesFromStale(t *testing.T) {
func TestRunSingleStageForceMarksDownstreamStaleAndRunContinuesFromStale(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -232,11 +238,11 @@ func TestRunStageForceMarksDownstreamStaleAndRunContinuesFromStale(t *testing.T)
}
var out bytes.Buffer
err = RunStage(context.Background(), []string{"polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"}, &out)
err = invokeNativeCommand(context.Background(), []string{"run", "--stage", "polish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force"}, &out)
if err != nil {
t.Fatalf("RunStage(force) error = %v", err)
t.Fatalf("single-stage run (force) error = %v", err)
}
if !strings.Contains(out.String(), "stage=polish executed=1 skipped=0 force=true") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "polish") {
t.Fatalf("output = %q, want forced polish rerun", out.String())
}
@@ -251,16 +257,16 @@ func TestRunStageForceMarksDownstreamStaleAndRunContinuesFromStale(t *testing.T)
}
out.Reset()
err = Run(context.Background(), []string{"2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err = invokeNativeCommand(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if !strings.Contains(out.String(), "executed=8 skipped=6") {
if !runCountsMatch(t, out.Bytes(), 8, 6, "") {
t.Fatalf("output = %q, want run to execute stale downstream stages", out.String())
}
}
func TestRunStageTrimExecutes(t *testing.T) {
func TestRunSingleStageTrimExecutes(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -269,12 +275,14 @@ func TestRunStageTrimExecutes(t *testing.T) {
mustWriteTestFile(t, finalPath, `{"segments":[{"id":1},{"id":2}]}`)
seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "normalize", artifacts.TranscriptOutputKindFinal, artifacts.ArtifactTranscriptFinal, finalPath)
seedInvocationPrerequisites(t, workspaceRoot, "trim")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"trim", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "trim", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage(trim) error = %v", err)
t.Fatalf("single-stage run (trim) error = %v", err)
}
if !strings.Contains(out.String(), "stage=trim executed=1 skipped=0") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "trim") {
t.Fatalf("output = %q, want stage=trim executed", out.String())
}
@@ -291,7 +299,7 @@ func TestRunStageTrimExecutes(t *testing.T) {
}
}
func TestRunStageNormalizeExecutes(t *testing.T) {
func TestRunSingleStageNormalizeExecutes(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -300,12 +308,14 @@ func TestRunStageNormalizeExecutes(t *testing.T) {
mustWriteTestFile(t, polishedPath, `{"segments":[{"id":1},{"id":2}]}`)
seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "polish", artifacts.TranscriptOutputKindPolished, artifacts.ArtifactTranscriptPolished, polishedPath)
seedInvocationPrerequisites(t, workspaceRoot, "normalize")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"normalize", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "normalize", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage(normalize) error = %v", err)
t.Fatalf("single-stage run (normalize) error = %v", err)
}
if !strings.Contains(out.String(), "stage=normalize executed=1 skipped=0") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "normalize") {
t.Fatalf("output = %q, want stage=normalize executed", out.String())
}
@@ -319,7 +329,7 @@ func TestRunStageNormalizeExecutes(t *testing.T) {
}
}
func TestRunStageRenderExecutes(t *testing.T) {
func TestRunSingleStageRenderExecutes(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
manifestPath := filepath.Join(workspaceRoot, "work", "sample-campaign", "2026-05-03", "manifest.json")
@@ -336,12 +346,14 @@ func TestRunStageRenderExecutes(t *testing.T) {
t.Fatal(err)
}
seedInvocationPrerequisites(t, workspaceRoot, "render")
var out bytes.Buffer
err := RunStage(context.Background(), []string{"render", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "render", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage(render) error = %v", err)
t.Fatalf("single-stage run (render) error = %v", err)
}
if !strings.Contains(out.String(), "stage=render executed=1 skipped=0") {
if !runCountsMatch(t, out.Bytes(), 1, 0, "render") {
t.Fatalf("output = %q, want stage=render executed", out.String())
}

View File

@@ -1,270 +0,0 @@
package app
import (
"context"
"flag"
"fmt"
"io"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/config"
)
// RunStage executes exactly one selected stage.
func RunStage(ctx context.Context, args []string, out io.Writer) error {
return runStageWithDependencies(ctx, args, out, executePlan, commandDependencies{})
}
func runStageWithPipelineExecutor(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor) error {
return runStageWithDependencies(ctx, args, out, executor, commandDependencies{})
}
func runStageWithDependencies(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
var stageName string
var positionalSessionID string
if len(args) >= 2 && !isCLIFlagToken(args[0]) && !isCLIFlagToken(args[1]) {
stageName = strings.TrimSpace(args[0])
positionalSessionID = strings.TrimSpace(args[1])
args = append([]string(nil), args[2:]...)
}
fs := flag.NewFlagSet("run-stage", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
var force bool
var selectedArtifacts artifactSelectionFlag
addCommonConfigFlags(fs, &flags)
fs.BoolVar(&force, "force", false, "rerun the stage even when already succeeded")
fs.Var(&selectedArtifacts, "artifacts", "configured artifact names to execute or publish (comma-separated or repeatable)")
if err := fs.Parse(args); err != nil {
return fmt.Errorf("run-stage: invalid flags: %w", err)
}
if stageName == "" {
switch fs.NArg() {
case 2:
stageName = strings.TrimSpace(fs.Arg(0))
positionalSessionID = strings.TrimSpace(fs.Arg(1))
case 1:
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("run-stage: expected stage name and session_id")
}
stageName = strings.TrimSpace(fs.Arg(0))
default:
return fmt.Errorf("run-stage: expected stage name and session_id")
}
} else if fs.NArg() != 0 {
return fmt.Errorf("run-stage: unexpected positional arguments")
}
if err := applyPositionalSessionID("run-stage", positionalSessionID, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("run-stage: session_id is required")
}
normalizedArtifacts, err := selectedArtifacts.Normalize()
if err != nil {
return fmt.Errorf("run-stage: invalid --artifacts: %w", err)
}
if len(normalizedArtifacts) > 0 && stageName != "analyze" && stageName != "publish" {
return fmt.Errorf("run-stage: --artifacts is only supported for stages \"analyze\" and \"publish\"")
}
summary, err := runSingleStageCommand(ctx, singleStageCommand{
CommandName: "run-stage",
StageName: stageName,
PipelinePath: flags.pipelinePath,
CampaignPath: flags.campaignPath,
CampaignFilePath: flags.campaignFilePath,
SessionPath: flags.sessionPath,
SessionID: flags.sessionID,
PreviousSessionID: flags.previousSessionID,
Overlay: flags.overlay.pointer(),
SecretsDir: flags.secretsDir.pointer(),
Force: force,
SelectedArtifacts: normalizedArtifacts,
}, executor, deps)
if err != nil {
return err
}
_, err = fmt.Fprintf(
out,
"narratio run-stage: stage=%s executed=%d skipped=%d force=%t; manifest=%s\n",
stageName,
len(summary.Executed),
len(summary.Skipped),
force,
summary.ManifestPath,
)
return err
}
// Analyze force-runs the analyze stage.
func Analyze(ctx context.Context, args []string, out io.Writer) error {
return analyzeWithDependencies(ctx, args, out, executePlan, commandDependencies{})
}
func analyzeWithPipelineExecutor(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor) error {
return analyzeWithDependencies(ctx, args, out, executor, commandDependencies{})
}
func analyzeWithDependencies(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
fs := flag.NewFlagSet("analyze", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
var selectedArtifacts artifactSelectionFlag
addCommonConfigFlags(fs, &flags)
fs.Var(&selectedArtifacts, "artifacts", "configured artifact names to execute during analyze (comma-separated or repeatable)")
if err := parseSessionAwareFlags("analyze", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("analyze: session_id is required")
}
normalizedArtifacts, err := selectedArtifacts.Normalize()
if err != nil {
return fmt.Errorf("analyze: invalid --artifacts: %w", err)
}
summary, err := runSingleStageCommand(ctx, singleStageCommand{
CommandName: "analyze",
StageName: "analyze",
PipelinePath: flags.pipelinePath,
CampaignPath: flags.campaignPath,
CampaignFilePath: flags.campaignFilePath,
SessionPath: flags.sessionPath,
SessionID: flags.sessionID,
PreviousSessionID: flags.previousSessionID,
Overlay: flags.overlay.pointer(),
SecretsDir: flags.secretsDir.pointer(),
Force: true,
SelectedArtifacts: normalizedArtifacts,
}, executor, deps)
if err != nil {
return err
}
_, err = fmt.Fprintf(
out,
"narratio analyze: executed=%d skipped=%d force=true; manifest=%s\n",
len(summary.Executed),
len(summary.Skipped),
summary.ManifestPath,
)
return err
}
// Publish force-runs the publish stage.
func Publish(ctx context.Context, args []string, out io.Writer) error {
return publishWithDependencies(ctx, args, out, executePlan, commandDependencies{})
}
func publishWithPipelineExecutor(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor) error {
return publishWithDependencies(ctx, args, out, executor, commandDependencies{})
}
func publishWithDependencies(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
fs := flag.NewFlagSet("publish", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var flags commonConfigFlags
var selectedArtifacts artifactSelectionFlag
addCommonConfigFlags(fs, &flags)
fs.Var(&selectedArtifacts, "artifacts", "configured artifact names to publish (comma-separated or repeatable)")
if err := parseSessionAwareFlags("publish", fs, args, &flags.sessionID); err != nil {
return err
}
if strings.TrimSpace(flags.sessionID) == "" {
return fmt.Errorf("publish: session_id is required")
}
normalizedArtifacts, err := selectedArtifacts.Normalize()
if err != nil {
return fmt.Errorf("publish: invalid --artifacts: %w", err)
}
summary, err := runSingleStageCommand(ctx, singleStageCommand{
CommandName: "publish",
StageName: "publish",
PipelinePath: flags.pipelinePath,
CampaignPath: flags.campaignPath,
CampaignFilePath: flags.campaignFilePath,
SessionPath: flags.sessionPath,
SessionID: flags.sessionID,
PreviousSessionID: flags.previousSessionID,
Overlay: flags.overlay.pointer(),
SecretsDir: flags.secretsDir.pointer(),
Force: true,
SelectedArtifacts: normalizedArtifacts,
}, executor, deps)
if err != nil {
return err
}
_, err = fmt.Fprintf(
out,
"narratio publish: executed=%d skipped=%d force=true; manifest=%s\n",
len(summary.Executed),
len(summary.Skipped),
summary.ManifestPath,
)
return err
}
type singleStageCommand struct {
CommandName string
StageName string
PipelinePath string
CampaignPath string
CampaignFilePath string
SessionPath string
SessionID string
PreviousSessionID string
Overlay *string
SecretsDir *string
Force bool
SelectedArtifacts []string
}
func runSingleStageCommand(ctx context.Context, req singleStageCommand, executor pipelineExecutor, deps commandDependencies) (*RunSummary, error) {
deps = normalizeCommandDependencies(deps)
plan, err := buildSingleStageExecutionPlan(req.StageName)
if err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
loaded, err := loadCommandConfigWithDependencies(ctx, req.PipelinePath, req.CampaignPath, req.CampaignFilePath, req.SessionPath, config.SessionLoadOptions{
SessionID: req.SessionID,
PreviousSessionID: req.PreviousSessionID,
Overlay: req.Overlay,
}, req.SecretsDir, deps)
if err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
defer func() { _ = loaded.Close() }()
cfg := loaded.Config
if err := config.Validate(cfg); err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
selectedArtifacts, err := normalizeArtifactSelection(cfg, req.SelectedArtifacts)
if err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
effectiveArtifacts, err := resolveEffectiveArtifacts(cfg, selectedArtifacts)
if err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
summary, err := executor(ctx, cfg, plan, RunOptions{
Force: req.Force,
SelectedArtifacts: selectedArtifacts,
EffectiveArtifacts: effectiveArtifacts,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", req.CommandName, err)
}
return summary, nil
}

View File

@@ -471,6 +471,7 @@ func TestNeedsObjectStoreForRunPrepareWithPreviousRequirements(t *testing.T) {
},
}
cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: cfg.Pipeline.Scriptorium.Artifacts}
got := needsObjectStoreForRun(cfg, []stage.Stage{countingStage{name: "prepare", runs: new(int)}})
if got != tt.want {
t.Fatalf("needsObjectStoreForRun() = %v, want %v", got, tt.want)

View File

@@ -48,7 +48,7 @@ func TestPipelineLoadUsesIndependentConventionalSecretsDirectory(t *testing.T) {
restoreEnvAfterTest(t, name)
mustWriteSecretFile(t, filepath.Join(dir, name), "synthetic-value\n")
pipelinePath := filepath.Join(t.TempDir(), "pipeline.yml")
if err := os.WriteFile(pipelinePath, []byte("notarius:\n enabled: false\n credentials: ["+name+"]\n"), 0o644); err != nil {
if err := os.WriteFile(pipelinePath, []byte("whisperx:\n transcribe_url: https://transcription.example.test/transcribe\nnotarius:\n enabled: false\n credentials: ["+name+"]\n"), 0o644); err != nil {
t.Fatal(err)
}
_, pipeline, err := loadPipelineConfigWithDependencies(pipelinePath, config.PipelineLoadOptions{}, nil, commandDependencies{})
@@ -216,7 +216,7 @@ func TestExecuteConfigShowReportsSuccessfulSecretsBootstrapOnStderr(t *testing.T
var stdout bytes.Buffer
var stderr bytes.Buffer
if code := executeForTest([]string{"config", "show", "--config", pipelinePath, "--secrets-dir", secretsDir}, &stdout, &stderr); code != 0 {
if code := executeForTest([]string{"config", "show", "--config", pipelinePath, "--secrets-dir", secretsDir, "--verbose"}, &stdout, &stderr); code != 0 {
t.Fatalf("config show exit = %d, stderr = %q", code, stderr.String())
}
if got := stderr.String(); !strings.Contains(got, `status="loaded"`) || !strings.Contains(got, `name="`+name+`"`) {
@@ -249,7 +249,7 @@ func TestExecuteConfigValidateReportsSkippedEmptyBeforeTypedLoadFailure(t *testi
var stdout bytes.Buffer
var stderr bytes.Buffer
if code := executeForTest([]string{"config", "validate", "--config", pipelinePath, "--secrets-dir", secretsDir}, &stdout, &stderr); code == 0 {
if code := executeForTest([]string{"config", "validate", "--config", pipelinePath, "--secrets-dir", secretsDir, "--verbose"}, &stdout, &stderr); code == 0 {
t.Fatal("config validate succeeded, want empty-secret failure")
}
got := stderr.String()

View File

@@ -1,95 +0,0 @@
package app
import (
"flag"
"fmt"
"strings"
)
func isCLIFlagToken(arg string) bool {
return strings.HasPrefix(arg, "-") && arg != "-"
}
func pullLeadingSessionID(args []string) (string, []string) {
if len(args) == 0 || isCLIFlagToken(args[0]) {
return "", args
}
rest := append([]string(nil), args[1:]...)
return strings.TrimSpace(args[0]), rest
}
func applyPositionalSessionID(command, positional string, sessionID *string) error {
positional = strings.TrimSpace(positional)
if positional == "" {
return nil
}
existing := strings.TrimSpace(*sessionID)
if existing != "" && existing != positional {
return fmt.Errorf("%s: positional session id %q does not match expected session id %q", command, positional, existing)
}
*sessionID = positional
return nil
}
func applyParsedSessionIDArg(command string, fs *flag.FlagSet, sessionID *string) error {
switch fs.NArg() {
case 0:
return nil
case 1:
return applyPositionalSessionID(command, fs.Arg(0), sessionID)
default:
return fmt.Errorf("%s: unexpected positional arguments", command)
}
}
func resolveParsedSessionID(command, positionalSessionID string, fs *flag.FlagSet, sessionID *string) error {
if strings.TrimSpace(positionalSessionID) == "" {
return applyParsedSessionIDArg(command, fs, sessionID)
}
if fs.NArg() != 0 {
return fmt.Errorf("%s: unexpected positional arguments", command)
}
return applyPositionalSessionID(command, positionalSessionID, sessionID)
}
func parseSessionAwareFlags(command string, fs *flag.FlagSet, args []string, sessionID *string) error {
positionalSessionID, args := pullLeadingSessionID(args)
if err := fs.Parse(args); err != nil {
return fmt.Errorf("%s: invalid flags: %w", command, err)
}
return resolveParsedSessionID(command, positionalSessionID, fs, sessionID)
}
func parseSessionIDAndOnePositionalArg(command, argName string, fs *flag.FlagSet, args []string, sessionID *string) (string, error) {
var positionalSessionID string
value := ""
if len(args) >= 2 && !isCLIFlagToken(args[0]) && !isCLIFlagToken(args[1]) {
positionalSessionID = strings.TrimSpace(args[0])
value = strings.TrimSpace(args[1])
args = append([]string(nil), args[2:]...)
}
if err := fs.Parse(args); err != nil {
return "", fmt.Errorf("%s: invalid flags: %w", command, err)
}
if value == "" {
switch fs.NArg() {
case 2:
positionalSessionID = strings.TrimSpace(fs.Arg(0))
value = strings.TrimSpace(fs.Arg(1))
case 1:
if strings.TrimSpace(*sessionID) == "" {
return "", fmt.Errorf("%s: expected session_id and %s", command, argName)
}
value = strings.TrimSpace(fs.Arg(0))
default:
return "", fmt.Errorf("%s: expected session_id and %s", command, argName)
}
} else if fs.NArg() != 0 {
return "", fmt.Errorf("%s: unexpected positional arguments", command)
}
if err := applyPositionalSessionID(command, positionalSessionID, sessionID); err != nil {
return "", err
}
return value, nil
}

View File

@@ -32,8 +32,7 @@ inputs:
t.Cleanup(func() { config.DefaultSessionConfigSearchPaths = origSessionDefaults })
var out bytes.Buffer
err := Plan(context.Background(), []string{
"2026-04-04",
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-04-04",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--previous-session-id", "2026-03-28",
@@ -54,7 +53,7 @@ func TestPlanFailsWhenSessionIDMismatchesConcreteSession(t *testing.T) {
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var out bytes.Buffer
err := Plan(context.Background(), []string{"2026-04-04", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-04-04", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err == nil {
t.Fatal("expected error, got nil")
}
@@ -81,11 +80,9 @@ inputs:
}
var out bytes.Buffer
err := Plan(context.Background(), []string{
"2026-05-03",
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--previous-session-id", "2026-04-25",
}, &out)
if err == nil {
@@ -101,11 +98,9 @@ func TestPlanRequiresExpectedPreviousSessionID(t *testing.T) {
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var out bytes.Buffer
err := Plan(context.Background(), []string{
"2026-05-03",
err := invokeNativeCommand(context.Background(), []string{"session", "plan", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--previous-session-id", "2026-04-26",
}, &out)
if err == nil {
@@ -116,16 +111,16 @@ func TestPlanRequiresExpectedPreviousSessionID(t *testing.T) {
}
}
func TestRunStageAcceptsPositionalSessionIDAndParsesStageName(t *testing.T) {
func TestRunSingleStageAcceptsPositionalSessionIDAndParsesStageName(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var out bytes.Buffer
err := RunStage(context.Background(), []string{"prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &out)
err := invokeNativeCommand(context.Background(), []string{"run", "--stage", "prepare", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &out)
if err != nil {
t.Fatalf("RunStage() error = %v", err)
t.Fatalf("single-stage run error = %v", err)
}
if !strings.Contains(out.String(), "stage=prepare") {
if report := decodeNativeReport[runReport](t, out.Bytes()); len(report.Stages) != 1 || report.Stages[0] != "prepare" || !report.Complete {
t.Fatalf("output = %q, want stage output", out.String())
}
}

View File

@@ -63,13 +63,13 @@ func sessionInitializationCommand(deps commandDependencies) commandkit.Command {
commandkit.ValueFlag(commandkit.FlagMeta{Name: "title", Usage: "Session title."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
(*input).Title = value.Value
}),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "output", Usage: "Local output YAML file."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
commandkit.ValueFlag(commandkit.FlagMeta{Name: "output", Completion: commandkit.CompleteFiles(), Usage: "Local output YAML file."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
(*input).Output = value.Value
}),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "audio-s3-prefix", Usage: "Session audio prefix in object storage."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
(*input).AudioS3Prefix = value.Value
}),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "audio-dir", Usage: "Local audio directory, relative to the invocation directory."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
commandkit.ValueFlag(commandkit.FlagMeta{Name: "audio-dir", Completion: commandkit.CompleteDirectories(), Usage: "Local audio directory, relative to the invocation directory."}, nil, stringValue, func(input **sessionInitializationInput, value commandkit.Supplied[string]) {
(*input).AudioDir = value.Value
}),
commandkit.BoolFlag(commandkit.FlagMeta{Name: "remote", Usage: "Create session configuration in object storage."}, false, func(input **sessionInitializationInput, value commandkit.Supplied[bool]) {
@@ -176,7 +176,7 @@ func initializeSession(ctx context.Context, request *sessionInitializationInput,
return report, nil
}
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, nil, deps)
store, err := newCommandObjectStoreWithDependencies(ctx, cfg, deps)
if err != nil {
return report, fmt.Errorf("session init: %w", err)
}

View File

@@ -18,7 +18,7 @@ func TestSessionInitMaterializesLocalTemplateImports(t *testing.T) {
fragment := filepath.Join(filepath.Dir(campaign), "shared.yml")
mustWriteTestFile(t, fragment, "title: Shared metadata\n")
output := filepath.Join(t.TempDir(), "nested", "session.yml")
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}, commandDependencies{}); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &bytes.Buffer{}, executePlan, commandDependencies{}); err != nil {
t.Fatal(err)
}
if err := os.Remove(fragment); err != nil {
@@ -39,7 +39,7 @@ func TestSessionInitPublishesStandaloneImportedTemplate(t *testing.T) {
fragment := filepath.Join(filepath.Dir(campaign), "shared.yml")
mustWriteTestFile(t, fragment, "title: Shared metadata\n")
fake := &storage.FakeBackend{}
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--remote"}, &bytes.Buffer{}, restoreCommandDependencies(fake)); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--remote"}, &bytes.Buffer{}, executePlan, restoreCommandDependencies(fake)); err != nil {
t.Fatal(err)
}
key := artifacts.S3SessionConfigKey(artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-06-07"))
@@ -58,7 +58,7 @@ func TestSessionInitPublishesStandaloneImportedTemplate(t *testing.T) {
func TestSessionInitPersistsInvocationRelativeAudio(t *testing.T) {
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
output := filepath.Join(t.TempDir(), "nested", "session.yml")
if err := sessionInitWithDependencies(context.Background(), []string{"2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output, "--audio-dir", "./audio"}, &bytes.Buffer{}, commandDependencies{}); err != nil {
if err := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output, "--audio-dir", "./audio"}, &bytes.Buffer{}, executePlan, commandDependencies{}); err != nil {
t.Fatal(err)
}
loaded, err := config.LoadSession(output)

View File

@@ -33,13 +33,12 @@ func TestExecuteRunAcceptsPositionalSessionID(t *testing.T) {
"run",
"2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, executor)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "narratio run: session 2026-05-03") {
if report := decodeNativeReport[runReport](t, stdout.Bytes()); report.SessionID != "2026-05-03" || !report.Complete {
t.Fatalf("stdout = %q, want run summary", stdout.String())
}
if capturedSessionID != "2026-05-03" {
@@ -57,8 +56,7 @@ func TestExecutePositionalSessionIDMismatchFails(t *testing.T) {
"run",
"2026-05-04",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
@@ -68,39 +66,6 @@ func TestExecutePositionalSessionIDMismatchFails(t *testing.T) {
}
}
func TestExecuteSessionIDFlagMismatchFails(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{"session", "status", "2026-05-03", "--session-id", "2026-05-04"}, &stdout, &stderr)
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "does not match expected session id") {
t.Fatalf("stderr = %q, want positional/flag mismatch", stderr.String())
}
}
func TestExecuteSessionIDFlagAcceptedWithoutPositional(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
var stdout bytes.Buffer
var stderr bytes.Buffer
code := executeForTest([]string{
"session", "status",
"--session-id", "2026-05-03",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
}, &stdout, &stderr)
if code != 0 {
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
}
if !strings.Contains(stdout.String(), "Session: 2026-05-03") {
t.Fatalf("stdout = %q, want status output", stdout.String())
}
}
func TestExecuteRemoteSessionFallbackUsesPositionalSessionID(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)
@@ -159,25 +124,25 @@ func TestExecuteWorkflowCommandsAcceptPositionalSessionID(t *testing.T) {
}{
{
name: "run",
args: []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
args: []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
wantStage: "prepare",
wantForce: false,
},
{
name: "analyze",
args: []string{"analyze", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
name: "forced analyze",
args: []string{"run", "--stage", "analyze", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
wantStage: "analyze",
wantForce: true,
},
{
name: "publish",
args: []string{"publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
name: "forced publish",
args: []string{"run", "--stage", "publish", "--force", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
wantStage: "publish",
wantForce: true,
},
{
name: "run-stage",
args: []string{"run-stage", "publish", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--artifacts", "session_recap"},
name: "selected publish",
args: []string{"run", "2026-05-03", "--stage", "publish", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--artifacts", "session_recap"},
wantStage: "publish",
wantForce: false,
},
@@ -213,7 +178,7 @@ func TestExecuteWorkflowCommandsAcceptPositionalSessionID(t *testing.T) {
if capturedForce != tt.wantForce {
t.Fatalf("captured force = %t, want %t", capturedForce, tt.wantForce)
}
if tt.name == "analyze" || tt.name == "publish" || tt.name == "run-stage" {
if tt.name == "forced analyze" || tt.name == "forced publish" || tt.name == "selected publish" {
if strings.Join(capturedArtifacts, ",") != "session_recap" {
t.Fatalf("captured artifacts = %#v, want [session_recap]", capturedArtifacts)
}
@@ -226,43 +191,33 @@ func TestExecuteSessionSubcommandsAcceptPositionalSessionID(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
fake := &storage.FakeBackend{}
sessionPrefix := artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-05-03")
manifestKey := artifacts.S3RunSessionManifestKey(sessionPrefix, "20260519T010203Z-a1b2c3d4")
runIDKey := artifacts.S3CurrentCommitPointerKey(sessionPrefix)
fake.SeedObject(storage.FakeObject{Key: runIDKey, Data: []byte("20260519T010203Z-a1b2c3d4\n")})
fake.SeedObject(storage.FakeObject{Key: manifestKey, Data: restoreManifestJSON(t, "2026-05-03", "sample-campaign")})
seedOperatorCommittedOutputs(t, fake, nil)
var storeInitCalls int
deps := fakeCommandDependencies(fake, &storeInitCalls)
tests := []struct {
name string
args []string
want string
}{
{
name: "validate",
args: []string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
want: "OK config",
args: []string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
},
{
name: "status",
args: []string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
want: "Session: 2026-05-03",
args: []string{"session", "status", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
},
{
name: "plan",
args: []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
want: "narratio session plan: read-only workdir",
args: []string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
},
{
name: "artifacts",
args: []string{"session", "artifacts", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
want: "Built-in:",
args: []string{"session", "artifacts", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
},
{
name: "locks",
args: []string{"session", "locks", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath},
want: "Publish locks:",
args: []string{"session", "locks", "list", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath},
},
}
@@ -274,8 +229,8 @@ func TestExecuteSessionSubcommandsAcceptPositionalSessionID(t *testing.T) {
if code != 0 {
t.Fatalf("exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), tt.want) {
t.Fatalf("stdout = %q, want %q", stdout.String(), tt.want)
if report := decodeNativeReport[map[string]any](t, stdout.Bytes()); report["session_id"] != "2026-05-03" {
t.Fatalf("unexpected session identity in stdout = %q", stdout.String())
}
})
}
@@ -318,8 +273,7 @@ func TestExecuteSessionLocksMutationAcceptsPositionalSessionID(t *testing.T) {
code := executeWithDependencies([]string{
"session", "locks", "add", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
"--reason", "review",
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
@@ -339,8 +293,7 @@ func TestExecuteSessionLocksMutationAcceptsPositionalSessionID(t *testing.T) {
code = executeWithDependencies([]string{
"session", "locks", "remove", "2026-05-03", "narratio.transcript.final_trimmed",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--campaign-file", campaignPath, "--session-file", sessionPath,
}, &stdout, &stderr, deps, executePlan)
if code != 0 {
t.Fatalf("locks remove exit code = %d, want 0; stderr=%q", code, stderr.String())

View File

@@ -1,30 +0,0 @@
package app
import (
"bytes"
"context"
"strings"
"testing"
)
func TestStatusCommandRequiresSessionID(t *testing.T) {
var out bytes.Buffer
err := Status(context.Background(), nil, &out)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "status: session_id is required") {
t.Fatalf("error = %q, want missing session_id error", err.Error())
}
}
func TestStatusCommandRejectsManifestFlag(t *testing.T) {
var out bytes.Buffer
err := Status(context.Background(), []string{"2026-05-03", "--manifest", "manifest.json"}, &out)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "status: invalid flags: flag provided but not defined: -manifest") {
t.Fatalf("error = %q, want invalid manifest flag", err.Error())
}
}

View File

@@ -495,11 +495,11 @@ scriptorium:
inherited := load(t, false)
expanded := load(t, true)
inheritedValues, err := EffectivePipelineValues(inherited.Pipeline)
inheritedValues, err := MarshalEffectivePipeline(inherited.Pipeline)
if err != nil {
t.Fatal(err)
}
expandedValues, err := EffectivePipelineValues(expanded.Pipeline)
expandedValues, err := MarshalEffectivePipeline(expanded.Pipeline)
if err != nil {
t.Fatal(err)
}

View File

@@ -7,7 +7,6 @@ import (
)
const (
pipelineDefaultOwnershipSource = "default"
effectivePipelineDigestVersion = "narratio-effective-pipeline-v4\n"
)

View File

@@ -33,25 +33,6 @@ func NormalizedConfigurationPath(path string) string {
return normalizedProvenancePath(path)
}
// EffectivePipelineValueRecord identifies one normalized, secret-free
// effective configuration value. Values use deterministic compact JSON
// representations so command output can be compared without raw YAML layout.
type EffectivePipelineValueRecord struct {
Path string
Value string
}
// EffectivePipelineValues projects a fully resolved pipeline into sorted
// logical configuration values. Mappings are flattened, while sequences remain
// atomic values. The projection uses the same normalized effective mapping as
// persistence and informational identity; it is not a native inspection view.
func EffectivePipelineValues(cfg *PipelineConfig) ([]EffectivePipelineValueRecord, error) {
if cfg == nil {
return nil, fmt.Errorf("pipeline config is required")
}
return projectionValueRecords(projectEffectivePipeline(cfg))
}
// MarshalEffectivePipeline materializes completed configuration for persistence.
// Credentials retain reference names; domain catalogs remain separate.
func MarshalEffectivePipeline(cfg *PipelineConfig) ([]byte, error) {

View File

@@ -1,11 +1,8 @@
package config
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
)
// effectivePipelineProjection safely materializes configuration for persistence
@@ -78,11 +75,6 @@ type effectiveScriptoriumConfig struct {
Artifacts map[string]ScriptoriumArtifactConfig `yaml:"artifacts" json:"artifacts"`
}
type projectedPipelineValue struct {
record EffectivePipelineValueRecord
segments []string
}
func projectEffectivePipeline(cfg *PipelineConfig) effectivePipelineProjection {
projection := effectivePipelineProjection{
Workspace: cfg.Workspace, Campaigns: cfg.Campaigns, Storage: effectiveStorageConfig{Backend: cfg.Storage.Backend},
@@ -118,65 +110,3 @@ func projectionJSON(projection effectivePipelineProjection) ([]byte, error) {
}
return data, nil
}
func projectionValueTree(value any) (any, error) {
data, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("encode typed pipeline projection: %w", err)
}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
var root any
if err := decoder.Decode(&root); err != nil {
return nil, fmt.Errorf("decode effective pipeline projection: %w", err)
}
return root, nil
}
func projectionValueRecords(projection effectivePipelineProjection) ([]EffectivePipelineValueRecord, error) {
detailed, err := projectionDetailedRecords(projection)
if err != nil {
return nil, err
}
records := make([]EffectivePipelineValueRecord, 0, len(detailed))
for _, value := range detailed {
records = append(records, value.record)
}
return records, nil
}
func projectionDetailedRecords(value any) ([]projectedPipelineValue, error) {
decoded, err := projectionValueTree(value)
if err != nil {
return nil, err
}
root, ok := decoded.(map[string]any)
if !ok {
return nil, fmt.Errorf("effective pipeline projection must be a mapping")
}
var records []projectedPipelineValue
appendProjectionRecords(&records, nil, root)
sort.Slice(records, func(i, j int) bool { return records[i].record.Path < records[j].record.Path })
return records, nil
}
func appendProjectionRecords(records *[]projectedPipelineValue, segments []string, value any) {
if mapping, ok := value.(map[string]any); ok && len(mapping) > 0 {
keys := make([]string, 0, len(mapping))
for key := range mapping {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
child := append(append([]string(nil), segments...), key)
appendProjectionRecords(records, child, mapping[key])
}
return
}
encoded, _ := json.Marshal(value)
path := strings.Join(segments, ".")
*records = append(*records, projectedPipelineValue{
record: EffectivePipelineValueRecord{Path: path, Value: string(encoded)},
segments: append([]string(nil), segments...),
})
}

View File

@@ -115,14 +115,6 @@ notarius:
if err != nil {
t.Fatal(err)
}
firstValues, err := EffectivePipelineValues(first)
if err != nil {
t.Fatal(err)
}
serializedValues := ""
for _, value := range firstValues {
serializedValues += value.Path + "=" + value.Value + "\n"
}
directYAML, err := yaml.Marshal(first)
if err != nil {
t.Fatal(err)
@@ -131,7 +123,7 @@ notarius:
if err != nil {
t.Fatal(err)
}
for _, output := range []string{string(firstYAML), serializedValues, string(directYAML), string(directJSON)} {
for _, output := range []string{string(firstYAML), string(directYAML), string(directJSON)} {
if strings.Contains(output, firstCanary) {
t.Fatalf("effective output exposed credential value: %q", output)
}
@@ -165,12 +157,12 @@ notarius:
if err != nil {
t.Fatal(err)
}
secondValues, err := EffectivePipelineValues(second)
secondYAML, err := MarshalEffectivePipeline(second)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(firstValues, secondValues) {
t.Fatalf("credential value change altered effective projection:\nfirst=%#v\nsecond=%#v", firstValues, secondValues)
if string(firstYAML) != string(secondYAML) {
t.Fatal("credential value change altered safe materialization")
}
if EffectivePipelineDigest(first) != EffectivePipelineDigest(second) {
t.Fatal("credential value change altered effective digest")
@@ -195,16 +187,12 @@ notarius:
}
}
func TestEffectivePipelineValuesPreserveNilEmptyAndExactIntegers(t *testing.T) {
func TestPipelineMaterializationPreservesNilEmptyAndExactIntegers(t *testing.T) {
base := &PipelineConfig{
Audita: AuditaConfig{Modules: nil},
Scriptorium: &ScriptoriumConfig{Artifacts: nil},
resolution: &pipelineResolutionMetadata{},
}
nilValues, err := EffectivePipelineValues(base)
if err != nil {
t.Fatal(err)
}
nilYAML, err := MarshalEffectivePipeline(base)
if err != nil {
t.Fatal(err)
@@ -237,19 +225,6 @@ func TestEffectivePipelineValuesPreserveNilEmptyAndExactIntegers(t *testing.T) {
base.Scriptorium.Artifacts = map[string]ScriptoriumArtifactConfig{
"recap": {Vars: map[string]any{"exact": int64(9007199254740993)}},
}
emptyValues, err := EffectivePipelineValues(base)
if err != nil {
t.Fatal(err)
}
if got := effectiveValueForTest(t, nilValues, "audita.modules"); got != "null" {
t.Fatalf("nil sequence value = %q, want null", got)
}
if got := effectiveValueForTest(t, emptyValues, "audita.modules"); got != "[]" {
t.Fatalf("empty sequence value = %q, want []", got)
}
if got := effectiveValueForTest(t, emptyValues, "scriptorium.artifacts.recap.vars.exact"); got != "9007199254740993" {
t.Fatalf("exact integer value = %q", got)
}
if err := recomputePipelineEffectiveDigest(base); err != nil {
t.Fatal(err)
}
@@ -262,17 +237,6 @@ func TestEffectivePipelineValuesPreserveNilEmptyAndExactIntegers(t *testing.T) {
}
}
func effectiveValueForTest(t *testing.T, values []EffectivePipelineValueRecord, path string) string {
t.Helper()
for _, value := range values {
if value.Path == path {
return value.Value
}
}
t.Fatalf("effective value %q not found", path)
return ""
}
func TestValidateMissingAudioSource(t *testing.T) {
cfg := loadedValidConfig(t)
cfg.Session.Inputs.AudioDir = ""

View File

@@ -1,6 +1,7 @@
package config
import (
"bytes"
"strings"
"testing"
@@ -103,21 +104,12 @@ func TestScriptoriumInheritsResolvedTypedCredentialsSafely(t *testing.T) {
t.Fatalf("%s serialization omitted the inherited Scriptorium credential", name)
}
}
values, err := EffectivePipelineValues(cfg)
if err != nil {
var inspection bytes.Buffer
if err := cfg.ConfigKitResult().WriteEffective(&inspection); err != nil {
t.Fatal(err)
}
inspection, err := yaml.Marshal(values)
if err != nil {
t.Fatal(err)
}
foundInheritedCredential := false
for _, value := range values {
if value.Path == "scriptorium.credentials" && strings.Contains(value.Value, envName) {
foundInheritedCredential = true
}
}
if !foundInheritedCredential || strings.Contains(string(inspection), secret) {
t.Fatal("effective inspection did not safely expose inherited credential identity")
if !strings.Contains(inspection.String(), envName) || strings.Contains(inspection.String(), secret) {
t.Fatal("native inspection did not safely expose inherited credential identity")
}
}