Build shared prompt inspection targets
This commit is contained in:
@@ -293,6 +293,8 @@ Check: `go test ./internal/app ./internal/stage`.
|
|||||||
|
|
||||||
## Stage 7: Build shared inspection targets
|
## Stage 7: Build shared inspection targets
|
||||||
|
|
||||||
|
**Status: Complete.**
|
||||||
|
|
||||||
**Purpose:** Select the right prompts and intended bindings for each entry point.
|
**Purpose:** Select the right prompts and intended bindings for each entry point.
|
||||||
|
|
||||||
Read the roadmap's Inspection Policies, Stage 5 context builders, Stage 6
|
Read the roadmap's Inspection Policies, Stage 5 context builders, Stage 6
|
||||||
|
|||||||
81
internal/app/preflight_targets.go
Normal file
81
internal/app/preflight_targets.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func executionInspectionTargets(cfg *config.Config, sessionID string, projection *workProjection) ([]preflight.Target, error) {
|
||||||
|
var trim bool
|
||||||
|
var analysis []string
|
||||||
|
for _, item := range projection.Stages {
|
||||||
|
if item.Decision.action != stageActionRun {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch item.Name {
|
||||||
|
case "trim":
|
||||||
|
trim = true
|
||||||
|
case "analyze":
|
||||||
|
if item.Analysis != nil {
|
||||||
|
for _, work := range item.Analysis.ExecutionOrder {
|
||||||
|
analysis = append(analysis, work.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buildInspectionTargets(cfg, sessionID, trim, analysis)
|
||||||
|
}
|
||||||
|
|
||||||
|
func validationInspectionTargets(cfg *config.Config, sessionID string) ([]preflight.Target, error) {
|
||||||
|
if cfg == nil || cfg.Pipeline == nil {
|
||||||
|
return nil, fmt.Errorf("resolved configuration is required")
|
||||||
|
}
|
||||||
|
var keys []string
|
||||||
|
if cfg.Pipeline.Scriptorium != nil {
|
||||||
|
selected, err := artifacts.ResolveEffectiveArtifactSet(artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts()), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
keys, err = stage.AnalyzeTargetClosure(cfg.ConcreteArtifacts(), selected.Keys())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buildInspectionTargets(cfg, sessionID, true, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildInspectionTargets(cfg *config.Config, sessionID string, trim bool, keys []string) ([]preflight.Target, error) {
|
||||||
|
if cfg == nil || cfg.Pipeline == nil {
|
||||||
|
return nil, fmt.Errorf("resolved configuration is required")
|
||||||
|
}
|
||||||
|
var targets []preflight.Target
|
||||||
|
trimCfg := cfg.Pipeline.Trim
|
||||||
|
if trim && trimCfg != nil && trimCfg.Enabled != nil && *trimCfg.Enabled {
|
||||||
|
context, err := stage.TrimScriptoriumContext(cfg, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("trim requirements: %w", err)
|
||||||
|
}
|
||||||
|
targets = append(targets, preflight.Target{Stage: "trim", Context: context, Inputs: map[string]preflight.Binding{trimCfg.Bounds.TranscriptInputName: {SourceID: artifacts.ArtifactTranscriptFinal, Required: true}}})
|
||||||
|
}
|
||||||
|
configured := cfg.ConcreteArtifacts()
|
||||||
|
for _, key := range keys {
|
||||||
|
artifact, ok := configured[key]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("analysis artifact %q is not configured", key)
|
||||||
|
}
|
||||||
|
context, err := stage.AnalyzeScriptoriumContext(cfg, artifact, sessionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("analysis artifact %q requirements: %w", key, err)
|
||||||
|
}
|
||||||
|
inputs := make(map[string]preflight.Binding, len(artifact.Inputs))
|
||||||
|
for name, input := range artifact.Inputs {
|
||||||
|
inputs[name] = preflight.Binding{SourceID: input.Source, Required: input.Required}
|
||||||
|
}
|
||||||
|
targets = append(targets, preflight.Target{Stage: "analyze", Artifact: key, Context: context, Inputs: inputs})
|
||||||
|
}
|
||||||
|
return targets, nil
|
||||||
|
}
|
||||||
129
internal/app/preflight_targets_test.go
Normal file
129
internal/app/preflight_targets_test.go
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInspectionTargetsUseProjectedWorkAndDefinitionClosure(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)
|
||||||
|
}
|
||||||
|
enabled := true
|
||||||
|
cfg.Pipeline.Trim.Enabled = &enabled
|
||||||
|
cfg.ArtifactCatalog.Artifacts = map[string]config.ScriptoriumArtifactConfig{
|
||||||
|
"parent": {Enabled: false, PromptID: "parent", OutputPath: "artifacts/parent.md"},
|
||||||
|
"child": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"parent"}, Inputs: map[string]config.ScriptoriumInputConfig{"transcript": {Source: artifacts.ArtifactTranscriptFinalTrimmed, Required: true}, "notes": {Source: "narratio.artifact.parent", Required: false}}},
|
||||||
|
"unrelated": {Enabled: false, PromptID: "other", OutputPath: "artifacts/other.md"},
|
||||||
|
}
|
||||||
|
selection := []string{"child"}
|
||||||
|
effective, err := resolveEffectiveArtifacts(cfg, selection)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
plan, err := BuildBoundedPlan("trim", "analyze")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
targets, err := executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(targets) != 3 || targets[0].Stage != "trim" || targets[1].Artifact != "parent" || targets[2].Artifact != "child" {
|
||||||
|
t.Fatalf("targets = %+v", targets)
|
||||||
|
}
|
||||||
|
if targets[2].Inputs["notes"].SourceID != "narratio.artifact.parent" || targets[2].Inputs["notes"].Required || targets[2].Inputs["transcript"].SourceID != artifacts.ArtifactTranscriptFinalTrimmed {
|
||||||
|
t.Fatalf("bindings = %+v", targets[2].Inputs)
|
||||||
|
}
|
||||||
|
if targets[2].Context.Vars["session_id"] != "narratio-session-"+cfg.Session.SessionID {
|
||||||
|
t.Fatalf("context = %+v", targets[2].Context)
|
||||||
|
}
|
||||||
|
validation, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||||
|
if err != nil || !reflect.DeepEqual(targets, validation) {
|
||||||
|
t.Fatalf("validation = %+v, error = %v", validation, err)
|
||||||
|
}
|
||||||
|
for i := range projection.Stages {
|
||||||
|
if projection.Stages[i].Name == "analyze" {
|
||||||
|
projection.Stages[i].Analysis.ExecutionOrder = projection.Stages[i].Analysis.ExecutionOrder[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
targets, err = executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
||||||
|
if err != nil || len(targets) != 2 || targets[1].Artifact != "child" {
|
||||||
|
t.Fatalf("reused prerequisite targets = %+v, error = %v", targets, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectionTargetsSkipDisabledTrimAndAbsentScriptorium(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
|
||||||
|
targets, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||||
|
if err != nil || len(targets) != 0 {
|
||||||
|
t.Fatalf("targets = %+v, error = %v", targets, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInspectionTargetsExpandSelectedFamilyAndIgnoreUnrelatedRange(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)
|
||||||
|
}
|
||||||
|
party, err := config.ParseParty([]byte("schema_version: narratio.party.v1\ncharacters:\n alice:\n player: {name: Player}\n character: {name: Alice, classes: [{name: wizard}]}\n"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cfg.Pipeline.Scriptorium.Artifacts = nil
|
||||||
|
cfg.Pipeline.Scriptorium.ArtifactFamilies = map[string]config.ScriptoriumArtifactFamilyConfig{"character": {Enabled: true, ForEach: "party.characters", PromptID: "character", OutputPathPattern: "artifacts/{character_id}.md", MemberVars: map[string]string{"character_name": "character.name"}}}
|
||||||
|
cfg.ArtifactCatalog, err = config.DeriveArtifactCatalog(cfg.Pipeline, config.ResolvedParty{Canonical: party})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
selected, err := normalizeArtifactSelection(cfg, []string{"character"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
effective, err := resolveEffectiveArtifacts(cfg, selected)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
plan, err := BuildBoundedPlan("analyze", "analyze")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selected, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
targets, err := executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
||||||
|
if err != nil || len(targets) != 1 || targets[0].Artifact != "character_alice" || targets[0].Context.Vars["character_name"] != "Alice" {
|
||||||
|
t.Fatalf("targets = %+v, error = %v", targets, err)
|
||||||
|
}
|
||||||
|
plan, err = BuildBoundedPlan("prepare", "prepare")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
projection, err = projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
targets, err = executionInspectionTargets(cfg, cfg.Session.SessionID, projection)
|
||||||
|
if err != nil || len(targets) != 0 {
|
||||||
|
t.Fatalf("unrelated targets = %+v, error = %v", targets, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
40
internal/preflight/types.go
Normal file
40
internal/preflight/types.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// Package preflight coordinates read-only checks of intended prompt requests.
|
||||||
|
package preflight
|
||||||
|
|
||||||
|
import "gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
|
|
||||||
|
// Binding identifies an intended prompt input and the source that supplies it.
|
||||||
|
type Binding struct {
|
||||||
|
SourceID string
|
||||||
|
Required bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Target identifies a concrete prompt request and its intended inputs.
|
||||||
|
type Target struct {
|
||||||
|
Stage string
|
||||||
|
Artifact string
|
||||||
|
Context scriptorium.RequestContext
|
||||||
|
Inputs map[string]Binding
|
||||||
|
}
|
||||||
|
|
||||||
|
type Severity string
|
||||||
|
|
||||||
|
const (
|
||||||
|
SeverityError Severity = "error"
|
||||||
|
SeverityWarning Severity = "warning"
|
||||||
|
SeverityInfo Severity = "info"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Finding reports a contextual result of requirements inspection.
|
||||||
|
type Finding struct {
|
||||||
|
Severity Severity
|
||||||
|
Stage string
|
||||||
|
Artifact string
|
||||||
|
PromptID string
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result holds findings collected across inspected targets.
|
||||||
|
type Result struct {
|
||||||
|
Findings []Finding
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user