Include prompt requirements in session validation
This commit is contained in:
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/logging"
|
||||
@@ -15,6 +16,7 @@ type commandDependencies struct {
|
||||
preparePipelineSource func(configkit.Source) (*configkit.SecretsReport, configkit.Source, error)
|
||||
loadPipelineSource func(configkit.Source, config.PipelineLoadOptions) (*config.PipelineConfig, error)
|
||||
newObjectStore func(context.Context, *config.Config) (storage.ObjectStore, error)
|
||||
newScriptorium func() scriptorium.Runner
|
||||
discoverRemoteCurrentState func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error)
|
||||
buildRestorePlan func(context.Context, *config.Config, *RemoteCurrentState, storage.ObjectStore, RestorePlanOptions) (*RestorePlan, error)
|
||||
executeRestorePlan func(context.Context, *config.Config, *RemoteCurrentState, *RestorePlan, *RestoreReport, storage.ObjectStore) (*RestoreExecutionResult, error)
|
||||
@@ -32,6 +34,9 @@ func normalizeCommandDependencies(deps commandDependencies) commandDependencies
|
||||
if deps.newObjectStore == nil {
|
||||
deps.newObjectStore = storage.NewObjectStoreFromConfig
|
||||
}
|
||||
if deps.newScriptorium == nil {
|
||||
deps.newScriptorium = defaultScriptoriumRunner
|
||||
}
|
||||
if deps.discoverRemoteCurrentState == nil {
|
||||
deps.discoverRemoteCurrentState = discoverRemoteCurrentState
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
@@ -444,7 +445,7 @@ inputs:
|
||||
fake := &storage.FakeBackend{}
|
||||
audioKey := artifacts.S3PublishedOutputKey(artifacts.S3AudioPrefix(artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-05-03"), "audio/"), "alice.flac")
|
||||
fake.SeedObject(storage.FakeObject{Key: audioKey, Data: []byte("audio")})
|
||||
deps := commandDependencies{newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
||||
deps := commandDependencies{newScriptorium: func() scriptorium.Runner { return &scriptorium.FakeRunner{} }, newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
||||
if os.Getenv(accessKeyEnv) != "test-key-id" || os.Getenv(secretKeyEnv) != "test-secret" {
|
||||
return nil, fmt.Errorf("secrets were not loaded before object store init")
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
@@ -450,7 +451,7 @@ func remoteSessionLoaderDependencies(fake *storage.FakeBackend) (commandDependen
|
||||
}
|
||||
|
||||
func fakeCommandDependencies(fake *storage.FakeBackend, storeInitCalls *int) commandDependencies {
|
||||
return commandDependencies{newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
||||
return commandDependencies{newScriptorium: func() scriptorium.Runner { return &scriptorium.FakeRunner{} }, newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) {
|
||||
if storeInitCalls != nil {
|
||||
*storeInitCalls++
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func (dependencies invocationDependencies) normalized() invocationDependencies {
|
||||
dependencies.newNotarius = func() notarius.Runner { return notarius.NewSubprocessRunner() }
|
||||
}
|
||||
if dependencies.newScriptorium == nil {
|
||||
dependencies.newScriptorium = func() scriptorium.Runner { return scriptorium.NewSubprocessRunner() }
|
||||
dependencies.newScriptorium = defaultScriptoriumRunner
|
||||
}
|
||||
if dependencies.newObjectStore == nil {
|
||||
dependencies.newObjectStore = func(ctx context.Context, cfg *config.Config) (storage.ObjectStore, error) {
|
||||
@@ -54,3 +54,5 @@ func (dependencies invocationDependencies) normalized() invocationDependencies {
|
||||
}
|
||||
return dependencies
|
||||
}
|
||||
|
||||
func defaultScriptoriumRunner() scriptorium.Runner { return scriptorium.NewSubprocessRunner() }
|
||||
|
||||
@@ -30,7 +30,7 @@ func inspectInvocationRequirements(runtime *invocationRuntime) error {
|
||||
result, err := preflight.Check(runtime.ctx, runtime.env.Scriptorium, targets)
|
||||
for _, finding := range result.Findings {
|
||||
if finding.Severity == preflight.SeverityWarning {
|
||||
runtime.env.Warn(finding.Message, "stage", finding.Stage, "artifact", finding.Artifact, "prompt", finding.PromptID, "issue", finding.IssueCode, "input", finding.InputName, "variable", finding.VariableName)
|
||||
runtime.env.Warn(formatPromptFinding(finding), "stage", finding.Stage, "artifact", finding.Artifact, "prompt", finding.PromptID, "issue", finding.IssueCode, "input", finding.InputName, "variable", finding.VariableName)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -38,3 +38,14 @@ func inspectInvocationRequirements(runtime *invocationRuntime) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatPromptFinding(item preflight.Finding) string {
|
||||
context := fmt.Sprintf("%s prompt %q", item.Stage, item.PromptID)
|
||||
if item.Artifact != "" {
|
||||
context = fmt.Sprintf("%s artifact %q prompt %q", item.Stage, item.Artifact, item.PromptID)
|
||||
}
|
||||
if item.IssueCode != "" {
|
||||
return fmt.Sprintf("%s: %s (issue=%s input=%q variable=%q)", context, item.Message, item.IssueCode, item.InputName, item.VariableName)
|
||||
}
|
||||
return context + ": " + item.Message
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
@@ -68,6 +69,7 @@ func validateSelectedSession(ctx context.Context, input *sessionInspectionInput,
|
||||
}
|
||||
|
||||
func inspectSessionValidation(ctx context.Context, cfg *config.Config, deps commandDependencies) (*sessionValidationReport, error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
report := &sessionValidationReport{Campaign: cfg.Session.Campaign, SessionID: cfg.Session.SessionID, SessionSource: sessionSourceSummary(cfg), Findings: []finding{}}
|
||||
var resultErr error
|
||||
fail := func(category string, err error) {
|
||||
@@ -76,8 +78,9 @@ func inspectSessionValidation(ctx context.Context, cfg *config.Config, deps comm
|
||||
resultErr = errors.Join(resultErr, err)
|
||||
}
|
||||
}
|
||||
if err := config.Validate(cfg); err != nil {
|
||||
fail("config", err)
|
||||
configErr := config.Validate(cfg)
|
||||
if configErr != nil {
|
||||
fail("config", configErr)
|
||||
} else {
|
||||
report.Findings = append(report.Findings, okFinding("config", "resolved pipeline, campaign, and session config"))
|
||||
}
|
||||
@@ -112,6 +115,34 @@ func inspectSessionValidation(ctx context.Context, cfg *config.Config, deps comm
|
||||
}
|
||||
effective, effectiveErr := resolveEffectiveArtifacts(cfg, nil)
|
||||
fail("config", effectiveErr)
|
||||
if configErr == nil && effectiveErr == nil {
|
||||
targets, targetErr := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
if targetErr != nil {
|
||||
fail("scriptorium", targetErr)
|
||||
} else if len(targets) > 0 {
|
||||
runner := deps.newScriptorium()
|
||||
checked, checkErr := preflight.Check(ctx, runner, targets)
|
||||
hasInspectionError := false
|
||||
for _, item := range checked.Findings {
|
||||
message := formatPromptFinding(item)
|
||||
switch item.Severity {
|
||||
case preflight.SeverityError:
|
||||
hasInspectionError = true
|
||||
report.Findings = append(report.Findings, errorFinding("scriptorium", message))
|
||||
case preflight.SeverityWarning:
|
||||
report.Findings = append(report.Findings, warnFinding("scriptorium", message))
|
||||
default:
|
||||
report.Findings = append(report.Findings, infoFinding("scriptorium", message))
|
||||
}
|
||||
}
|
||||
if checkErr != nil {
|
||||
resultErr = errors.Join(resultErr, checkErr)
|
||||
if !hasInspectionError {
|
||||
report.Findings = append(report.Findings, errorFinding("scriptorium", checkErr.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if effectiveErr == nil {
|
||||
previous := inspectPreviousArtifactReadiness(ctx, cfg, store, artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective))
|
||||
if len(previous.Requirements) == 0 {
|
||||
|
||||
142
internal/app/session_preflight_test.go
Normal file
142
internal/app/session_preflight_test.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"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/preflight"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type validationReportRunner struct{ scriptorium.FakeRunner }
|
||||
|
||||
func (r *validationReportRunner) InspectRequirements(ctx context.Context, req scriptorium.RequirementsRequest) (scriptorium.RequirementsReport, error) {
|
||||
r.RequirementsRequests = append(r.RequirementsRequests, req)
|
||||
if req.PromptID == "dnd.player_handout" {
|
||||
return scriptorium.RequirementsReport{Complete: true, Inputs: []scriptorium.RequirementInput{{Name: "recap", Kind: "required"}}}, nil
|
||||
}
|
||||
return scriptorium.RequirementsReport{Complete: true}, ctx.Err()
|
||||
}
|
||||
|
||||
func TestSessionValidationSharesPromptFindingsAndLeavesNoWorkspace(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
||||
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := &scriptorium.FakeRunner{RequirementsResult: &scriptorium.RequirementsReport{Complete: false, Inputs: []scriptorium.RequirementInput{{Name: "conditional", Kind: "conditional", Explanation: "unknown"}}}}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newScriptorium = func() scriptorium.Runner { return fake }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err != nil || !report.Valid || len(fake.RequirementsRequests) == 0 {
|
||||
t.Fatalf("report = %+v, error = %v", report, err)
|
||||
}
|
||||
targets, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
shared, err := preflight.Check(context.Background(), fake, targets)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, item := range shared.Findings {
|
||||
if !findingMatches(report.Findings, "WARN", "scriptorium", formatPromptFinding(item)) {
|
||||
t.Fatalf("validation omitted shared finding: %+v", item)
|
||||
}
|
||||
}
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, err := os.Stat(paths.Root); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("validation created workspace: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionValidationFailuresRemainFailuresInQuietMode(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
||||
want := errors.New("synthetic malformed requirements report")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
report *scriptorium.RequirementsReport
|
||||
inspectErr error
|
||||
expected string
|
||||
}{
|
||||
{"missing binding", &scriptorium.RequirementsReport{Complete: true, Inputs: []scriptorium.RequirementInput{{Name: "not_bound", Kind: "required"}}}, nil, "not_bound"},
|
||||
{"missing variable", &scriptorium.RequirementsReport{Complete: false, Variables: []scriptorium.RequirementVariable{{Name: "needed", Kind: "required"}}}, nil, "needed"},
|
||||
{"inspection failure", nil, want, "malformed requirements"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fake := &scriptorium.FakeRunner{RequirementsResult: tc.report, RequirementsErr: tc.inspectErr}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newScriptorium = func() scriptorium.Runner { return fake }
|
||||
app, err := newApplication(deps, executePlan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, diag bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"session", "validate", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--quiet", "--format", "json"}, commandkit.Streams{Out: &out, Err: &diag})
|
||||
if outcome.Status == 0 || outcome.Cause == nil || !strings.Contains(outcome.Cause.Error(), tc.expected) {
|
||||
t.Fatalf("outcome = %+v, diagnostics = %q", outcome, diag.String())
|
||||
}
|
||||
if tc.inspectErr != nil && !errors.Is(outcome.Cause, want) {
|
||||
t.Fatalf("cause = %v", outcome.Cause)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionValidationReportsOptionalSourceAndDisabledPrerequisite(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
||||
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parent := cfg.ArtifactCatalog.Artifacts["session_recap"]
|
||||
parent.Enabled = false
|
||||
cfg.ArtifactCatalog.Artifacts["session_recap"] = parent
|
||||
child := cfg.ArtifactCatalog.Artifacts["player_handout"]
|
||||
child.Inputs["recap"] = config.ScriptoriumInputConfig{Source: "narratio.artifact.session_recap", Required: false}
|
||||
cfg.ArtifactCatalog.Artifacts["player_handout"] = child
|
||||
fake := &validationReportRunner{}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newScriptorium = func() scriptorium.Runner { return fake }
|
||||
report, err := inspectSessionValidation(context.Background(), cfg, deps)
|
||||
if err != nil || !report.Valid {
|
||||
t.Fatalf("optional source warning should remain valid: report = %+v, err = %v", report, err)
|
||||
}
|
||||
if !findingMatches(report.Findings, "WARN", "scriptorium", "may be omitted") {
|
||||
t.Fatalf("missing optional-source warning: %+v", report.Findings)
|
||||
}
|
||||
seenParent := false
|
||||
for _, req := range fake.RequirementsRequests {
|
||||
if req.PromptID == "dnd.session_recap" {
|
||||
seenParent = true
|
||||
}
|
||||
}
|
||||
if !seenParent {
|
||||
t.Fatal("disabled prerequisite was not inspected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionValidationSkipsInspectorWithoutTargets(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||||
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
disabled := false
|
||||
cfg.Pipeline.Trim.Enabled = &disabled
|
||||
cfg.Pipeline.Scriptorium = nil
|
||||
cfg.ArtifactCatalog = nil
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newScriptorium = func() scriptorium.Runner { t.Fatal("unexpected inspector"); return nil }
|
||||
if _, err := inspectSessionValidation(context.Background(), cfg, deps); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func Check(ctx context.Context, inspector Inspector, targets []Target) (Result,
|
||||
report, err := inspector.InspectRequirements(ctx, target.Context)
|
||||
if err != nil {
|
||||
contextual := fmt.Errorf("%s: requirements inspection: %w", describeTarget(target), err)
|
||||
result.Findings = append(result.Findings, finding(target, SeverityError, contextual.Error()))
|
||||
result.Findings = append(result.Findings, finding(target, SeverityError, "requirements inspection: "+err.Error()))
|
||||
failures = append(failures, contextual)
|
||||
if ctx.Err() != nil {
|
||||
return result, errors.Join(append(failures, ctx.Err())...)
|
||||
|
||||
Reference in New Issue
Block a user