Check prompt requirements before stage attempts
This commit is contained in:
@@ -383,6 +383,9 @@ Check: `go test ./internal/preflight`.
|
|||||||
|
|
||||||
## Stage 9: Run preflight before stage attempts
|
## Stage 9: Run preflight before stage attempts
|
||||||
|
|
||||||
|
**Status: Complete.** Focused lifecycle and composition checks pass; the full
|
||||||
|
application suite still requires the offline fixture migration in Stage 11.
|
||||||
|
|
||||||
**Purpose:** Catch actionable Scriptorium problems before expensive work while
|
**Purpose:** Catch actionable Scriptorium problems before expensive work while
|
||||||
preserving completed results on preflight failure.
|
preserving completed results on preflight failure.
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,9 @@ func executePlanWithDependencies(ctx context.Context, cfg *config.Config, plan B
|
|||||||
if err := initializeLockedInvocation(runtime); err != nil {
|
if err := initializeLockedInvocation(runtime); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := inspectInvocationRequirements(runtime); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := composeInvocation(runtime); err != nil {
|
if err := composeInvocation(runtime); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
40
internal/app/runner_preflight.go
Normal file
40
internal/app/runner_preflight.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||||
|
)
|
||||||
|
|
||||||
|
func inspectInvocationRequirements(runtime *invocationRuntime) error {
|
||||||
|
if !stagesContainAny(runtime.stages, "trim", "analyze") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
projection, err := projectSelectedWork(runtime.ctx, runtime.cfg, runtime.plan, runtime.opts.Force, runtime.opts.SelectedArtifacts, runtime.effectiveArtifacts, runtime.artifactStore, runtime.manifest)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("project prompt inspection work: %w", err)
|
||||||
|
}
|
||||||
|
targets, err := executionInspectionTargets(runtime.cfg, runtime.identity.SessionID, projection)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("select prompt inspection targets: %w", err)
|
||||||
|
}
|
||||||
|
if len(targets) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if runtime.env.Scriptorium == nil {
|
||||||
|
runtime.env.Scriptorium = runtime.dependencies.newScriptorium()
|
||||||
|
}
|
||||||
|
if runtime.env.Scriptorium == nil {
|
||||||
|
return fmt.Errorf("initialize scriptorium runner: no runner available")
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("scriptorium preflight: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
137
internal/app/runner_preflight_test.go
Normal file
137
internal/app/runner_preflight_test.go
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type observingInspector struct {
|
||||||
|
scriptorium.FakeRunner
|
||||||
|
inspect func(scriptorium.RequirementsRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *observingInspector) InspectRequirements(ctx context.Context, req scriptorium.RequirementsRequest) (scriptorium.RequirementsReport, error) {
|
||||||
|
if r.inspect != nil {
|
||||||
|
r.inspect(req)
|
||||||
|
}
|
||||||
|
return r.FakeRunner.InspectRequirements(ctx, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
type warningRecorder struct{ warnings []string }
|
||||||
|
|
||||||
|
func (*warningRecorder) Info(string, ...any) {}
|
||||||
|
func (r *warningRecorder) Warn(message string, _ ...any) { r.warnings = append(r.warnings, message) }
|
||||||
|
func (*warningRecorder) Debug(string, ...any) {}
|
||||||
|
func (*warningRecorder) Progress(string, ...any) {}
|
||||||
|
|
||||||
|
func preflightTestConfig(t *testing.T) *config.Config {
|
||||||
|
t.Helper()
|
||||||
|
cfg := testConfig(t)
|
||||||
|
configured := map[string]config.ScriptoriumArtifactConfig{"recap": {Enabled: true, PromptID: "recap", OutputPath: "artifacts/recap.md"}}
|
||||||
|
cfg.Pipeline.Scriptorium = &config.ScriptoriumConfig{Binary: "scriptorium", Timeout: "1m", Artifacts: configured}
|
||||||
|
cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: configured}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreflightRunsBeforeAttemptsAndWarningsAllowExecution(t *testing.T) {
|
||||||
|
cfg := preflightTestConfig(t)
|
||||||
|
var earlierRuns int
|
||||||
|
reporter := &warningRecorder{}
|
||||||
|
runner := &observingInspector{inspect: func(req scriptorium.RequirementsRequest) {
|
||||||
|
if earlierRuns != 0 {
|
||||||
|
t.Fatal("inspection ran after first attempt")
|
||||||
|
}
|
||||||
|
if req.PromptID != "recap" {
|
||||||
|
t.Fatalf("request = %+v", req)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
runner.RequirementsResult = &scriptorium.RequirementsReport{Complete: false}
|
||||||
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &earlierRuns}, mustSelectStage(t, "analyze")}, RunOptions{Env: &Env{Scriptorium: runner}, Reporter: reporter})
|
||||||
|
if err != nil || summary == nil || !summary.Complete || earlierRuns != 1 || len(runner.RequirementsRequests) != 1 || len(runner.RunRequests) != 1 || len(reporter.warnings) == 0 {
|
||||||
|
t.Fatalf("summary = %+v, error = %v, warnings = %v", summary, err, reporter.warnings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreflightFailurePreservesCompletedAuthorityOnForce(t *testing.T) {
|
||||||
|
cfg := preflightTestConfig(t)
|
||||||
|
analyze := mustSelectStage(t, "analyze")
|
||||||
|
initial := &scriptorium.FakeRunner{}
|
||||||
|
if _, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Env: &Env{Scriptorium: initial}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
reused := &scriptorium.FakeRunner{RequirementsErr: errors.New("unexpected inspection")}
|
||||||
|
if _, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Env: &Env{Scriptorium: reused}}); err != nil || len(reused.RequirementsRequests) != 0 {
|
||||||
|
t.Fatalf("reusable work inspected: requests = %+v, error = %v", reused.RequirementsRequests, err)
|
||||||
|
}
|
||||||
|
store := &manifest.LocalStore{}
|
||||||
|
before, err := store.Load(context.Background(), manifestPathFor(cfg))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
output := filepath.Join(cfg.Pipeline.Workspace.Root, "work", cfg.Session.Campaign, cfg.Session.SessionID, "artifacts", "recap.md")
|
||||||
|
priorBytes, err := os.ReadFile(output)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runner := &scriptorium.FakeRunner{RequirementsResult: &scriptorium.RequirementsReport{Complete: true, Inputs: []scriptorium.RequirementInput{{Name: "transcript", Kind: "required"}}}}
|
||||||
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{analyze}, RunOptions{Force: true, Env: &Env{Scriptorium: runner}})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), `required input "transcript"`) || summary == nil || summary.StageResults[0].Attempted || len(runner.RunRequests) != 0 {
|
||||||
|
t.Fatalf("summary = %+v, error = %v", summary, err)
|
||||||
|
}
|
||||||
|
after, err := store.Load(context.Background(), manifestPathFor(cfg))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(before.Stages["analyze"], after.Stages["analyze"]) {
|
||||||
|
t.Fatalf("analysis authority changed: before=%+v after=%+v", before.Stages["analyze"], after.Stages["analyze"])
|
||||||
|
}
|
||||||
|
afterBytes, err := os.ReadFile(output)
|
||||||
|
if err != nil || !reflect.DeepEqual(priorBytes, afterBytes) {
|
||||||
|
t.Fatalf("output changed, error = %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(summaryRunRoot(cfg, summary.RunID)); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Fatalf("preflight created run directory: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreflightReusesInvocationOwnedRunner(t *testing.T) {
|
||||||
|
cfg := preflightTestConfig(t)
|
||||||
|
fake := &scriptorium.FakeRunner{}
|
||||||
|
created := 0
|
||||||
|
deps := invocationDependencies{newScriptorium: func() scriptorium.Runner { created++; return fake }}
|
||||||
|
plan := BoundedPlan{stages: []stage.Stage{mustSelectStage(t, "analyze")}}
|
||||||
|
_, err := executePlanWithDependencies(context.Background(), cfg, plan, RunOptions{}, deps)
|
||||||
|
if err != nil || created != 1 || len(fake.RequirementsRequests) != 1 || len(fake.RunRequests) != 1 {
|
||||||
|
t.Fatalf("created = %d, inspection = %d, generation = %d, error = %v", created, len(fake.RequirementsRequests), len(fake.RunRequests), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func summaryRunRoot(cfg *config.Config, runID string) string {
|
||||||
|
return filepath.Join(cfg.Pipeline.Workspace.Root, "work", cfg.Session.Campaign, cfg.Session.SessionID, "runs", runID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreflightInspectionFailureAndUnrelatedSelection(t *testing.T) {
|
||||||
|
cfg := preflightTestConfig(t)
|
||||||
|
want := errors.New("inspector unavailable")
|
||||||
|
runner := &scriptorium.FakeRunner{RequirementsErr: want}
|
||||||
|
var attempts int
|
||||||
|
summary, err := executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &attempts}, mustSelectStage(t, "analyze")}, RunOptions{Env: &Env{Scriptorium: runner}})
|
||||||
|
if !errors.Is(err, want) || attempts != 0 || summary == nil || summary.StageResults[0].Attempted {
|
||||||
|
t.Fatalf("summary = %+v, attempts = %d, error = %v", summary, attempts, err)
|
||||||
|
}
|
||||||
|
runner.RequirementsRequests = nil
|
||||||
|
_, err = executeStages(context.Background(), cfg, []stage.Stage{countingStage{name: "prepare", runs: &attempts}}, RunOptions{Force: true, Env: &Env{Scriptorium: runner}})
|
||||||
|
if err != nil || len(runner.RequirementsRequests) != 0 {
|
||||||
|
t.Fatalf("unrelated run error = %v, requests = %+v", err, runner.RequirementsRequests)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
@@ -373,6 +374,7 @@ func TestRefinementSemanticChangesRespectDependencyBranches(t *testing.T) {
|
|||||||
}, wantRuns: [7]int{0, 0, 0, 0, 1, 1, 1}, wantExtractStatus: manifest.StatusStale},
|
}, wantRuns: [7]int{0, 0, 0, 0, 1, 1, 1}, wantExtractStatus: manifest.StatusStale},
|
||||||
{name: "trim prompt", mutate: func(cfg *config.Config) {
|
{name: "trim prompt", mutate: func(cfg *config.Config) {
|
||||||
enabled := true
|
enabled := true
|
||||||
|
cfg.Pipeline.Scriptorium = &config.ScriptoriumConfig{Binary: "scriptorium", Timeout: "1m"}
|
||||||
cfg.Pipeline.Trim = &config.TrimConfig{
|
cfg.Pipeline.Trim = &config.TrimConfig{
|
||||||
Enabled: &enabled,
|
Enabled: &enabled,
|
||||||
Bounds: config.TrimBoundsConfig{PromptID: "session-bounds-v2"},
|
Bounds: config.TrimBoundsConfig{PromptID: "session-bounds-v2"},
|
||||||
@@ -408,7 +410,7 @@ func TestRefinementSemanticChangesRespectDependencyBranches(t *testing.T) {
|
|||||||
for index, name := range names {
|
for index, name := range names {
|
||||||
selected = append(selected, semanticContractRunStub{name: name, provider: providers[index], runs: &runs[index]})
|
selected = append(selected, semanticContractRunStub{name: name, provider: providers[index], runs: &runs[index]})
|
||||||
}
|
}
|
||||||
if _, err := executeStages(context.Background(), cfg, selected, RunOptions{}); err != nil {
|
if _, err := executeStages(context.Background(), cfg, selected, RunOptions{Env: &Env{Scriptorium: &scriptorium.FakeRunner{}}}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if runs != test.wantRuns {
|
if runs != test.wantRuns {
|
||||||
|
|||||||
Reference in New Issue
Block a user